I can't figure out why MySQL is returning an empty set for only certain users in the following query. I have the same info entered in every single profile, and only a couple userid's will return a result.
SELECT userinfo.userid, userinfo.location, locations.locationsName, locations.locationsID FROM userinfo
INNER JOIN locations ON locations.locationsID = userinfo.userid
WHERE userinfo.userid = '$userid'
Here are my tables (there are more columns, but they're irrelevant to this search)
userinfo
userid | location
locations
locationsID | locationsName
For example, I can open PHPMyAdmin and type in that query (changing the $userid). I made sure every single username has the same info entered (aside from having a different userid).
Because you're joining the LocationID to the UserID.
Based on your given schema, you probably want to join userinfo.userid = location.userid
Related
I have a table called website that contains some data about websites. The columns of this table are: id, website, quick_url, user_id, status, etc.
Each website that is in the table was added by a user, which is is saved in the user_id column.
I have another table called blocks that has only 3 columns: id, user_id, website_id.
I want to get all the websites from the website table, that were not added by a given user_id, but also, only the websites that were not blocked by the given user_id. So, websites that were not added by a given user or blocked by him.
Here is what I've tried:
SELECT * FROM website LEFT OUTER JOIN blocks ON tbl_website.userid = blocks.user_id WHERE website.user_id = blocks.user_id AND blocks.user_id = NULL AND website,user_id != '177' LIMIT 500;
It doesn't give me the wanted results ...
First, I've tried to do it like this:
SELECT * FROM tbl_website WHERE id<>(SELECT website_id from tbl_website_blocks WHERE user_id = '177')
which makes much more sense for me than my previous query, but I get this error: Subquery returns more than 1 row
I guess you can't have a "loop in loop" in an SQL query.
I'm aware that I could do two queries, and filter the results, but I would like to do it as much as possible from the SQL language, so that I don't "overload" the server.
Any suggestions would be appreciated.
In your second query rewrite the condition on
WHERE id not in (SELECT website_id from.....)
with <> you can compare it with just one value but your select returns list of values, so you can use not in to get results that are different then the selected list of IDs
Instead of '<>', try 'Not In'
SELECT * FROM tbl_website
WHERE id Not In (SELECT website_id from tbl_website_blocks WHERE user_id = '177')
I should also add this query is not a Join.
I have 2 tables, a users table and a trade table.
Which look like:
The structure of my code right now is:
<?php
$history = mysqli_query($con, "SELECT * FROM .......");
while($row = mysqli_fetch_array($history)) {
echo("The sentence");
} ?>
Problem I'm facing is that I'm trying to echo the user_name which in one case has to be the receiver and other the person giving it.
Pro tip: Never use SELECT * in software unless you know exactly why you are doing so. In your case it is harmful.
I'm assuming your query is really against the user and trade tables you mentioned in your question.
First, recast your query using 21st century SQL, as follows:
SELECT *
FROM trade AS t
JOIN user AS s ON s.user_id = t.user_id_sender
WHERE s.facebook_id = $fbid
Second, use this to retrieve your user's names and the item id traded.
SELECT s.user_name AS sender,
r.user_name AS receiver,
t.trade_id AS item_id
FROM trade AS t
JOIN user AS s ON s.user_id = t.user_id_sender
JOIN user AS r ON r.user_id = t.user_id_receiver
WHERE s.facebook_id = $fbid
See how we JOIN the user table twice, with two different aliases s (for sender) and r (for receiver)? That's the trick to fetching both names from IDs.
See how we employ the aliases sender and receiver to disambiguate the two user_name columns in the result set?
Now, when you use the php fetch_array function, you'll end up with these elements in the array.
$history['sender']
$history['receiver']
$history['item_id']
The array index strings correspond to the alias names you specified in your SELECT clause in your query.
So, one reason to avoid SELECT * is that you can get more than one column with the same name, and that means fetch_array will eliminate those duplicates and so it will lose useful information from your result set.
Problem
I have two different tables, table names are team_request and teams. [team_request] have unique records of each team submitted and [teams] saves duplicate records with unique username's but same team data as in [team_request] for the user who accepted to be part of that team.
Expected result from below query:
I am running the below query to fetch the pending challenges or team request, which have a basic logic of fetch all the teams from [team_request] where that team doesn't exists in [teams] table or hasn't been ignored by that user.
Actually what query is doing:
It's fetching all the rows (teams) from table [team_request] which belongs to that user, no matter whether a user has excepted or ignored.
Also forgot to mention above fbC_status is a field in [team_request] which saves user's email address if that user has ignored to be part of the team.
Any help or guidance will be appreciated I just got stuck with it.
MySQL Query
SELECT *
FROM team_request
WHERE (c_emails LIKE '%joe#example.com%')
OR (c_emails LIKE '%9876543210%')
AND fbC_status NOT LIKE '%joe#example.com%'
AND fbC_status NOT LIKE '%9876543210%'
AND '%joe#example.com%' NOT IN
(SELECT username
FROM teams)
AND t_name NOT IN
(SELECT T.t_name
FROM team_request TR,
teams T
WHERE TR.t_name = T.t_name
AND T.username = '%joe#example.com%'
AND (TR.fbC_status NOT LIKE '%joe#example.com%'
AND TR.fbC_status NOT LIKE '%9876543210%'))
LIMIT 0, 30
I would suggest you rethink how your database is structured, your teams table appears to be pretty much a duplicate of team_requests but given the current structure try something like:
#phone would be your '9876543210'
#username would be your 'joe#example.com'
SELECT r.*
FROM team_request r
WHERE (r.c_emails = #username OR r.c_emails = #phone)
AND r.fbC_status <> r.c_emails
AND r.t_name NOT IN ( SELECT t.name
FROM teams t
WHERE /* t.username = r.c_emails otherwise: */
t.username = #username /* OR t.phone = #phone*/
)
Its fairly unclear what all your fields are used for so this is unlikely to work without changes. The commented out parts are what I suspect you should use, but I have no means of knowing whether or not they would work with your tables.
You can feel free to use your LIKE '%...%' however remember that if a user has the email at#email.com, when a user with the email acrobat#email.com also exists LIKE '%at#email.com%' is going to match them both.
I have a table for users. But when a user makes any changes to their profile, I store them in a temp table until I approve them. The data then is copied over to the live table and deleted from the temp table.
What I want to achieve is that when viewing the data in the admin panel, or in the page where the user can double check before submitting, I want to write a single query that will allow me to fetch the data from both tables where the id in both equals $userid. Then I want to display them a table form, where old value appears in the left column and the new value appears in the right column.
I've found some sql solutions, but I'm not sure how to use them in php to echo the results as the columns in both have the same name.
Adding AS to a column name will allow you to alias it to a different name.
SELECT table1.name AS name1, table2.name AS name2, ...
FROM table1
INNER JOIN table2
ON ...
If you use the AS SQL keyword, you can rename a column just for that query's result.
SELECT
`member.uid`,
`member.column` AS `oldvalue`,
`edit.column` AS `newvalue`
FROM member, edit
WHERE
`member.uid` = $userId AND
`edit.uid` = $userId;
Something along those lines should work for you. Although SQL is not my strong point, so I'm pretty sure that this query would not work as is, even on a table with the correct fields and values.
Here is your required query.
Let suppose you have for example name field in two tables. Table one login and table 2 information. Now
SELECT login.name as LoginName , information.name InofName
FROM login left join information on information.user_id = login.id
Now you can use LoginName and InofName anywhere you need.
Use MySQL JOIN. And you can get all data from 2 tables in one mysql query.
SELECT * FROM `table1`
JOIN `table2` ON `table1`.`userid` = `table2`.`userid`
WHERE `table1`.`userid` = 1
Would it be possible to store a "link" to another record inside a record? For example:
table USERS
id name link
-------------------------------------------------------
1 user1 [link to record with id=4 in table info]
So, in PHP, I could do something like this:
// connect to the database etc....
$query = "select * from users where id=1";
$result = mysql_query($query);
$another_result = mysql_result($result, 0, 'link');
So that $another_result stores the result of another query, in the same raw format as if it was called using mysql_query().
Is this possible?
$query = "select info.* from info
inner join users on users.link = info.id
where users.id=1";
$result = mysql_query($query);
Using JOIN is a fundamental part of SQL, like using for loops in PHP.
Read A Visual Explanation of SQL Joins by fearless leader.
Maybe you mean a key that point to a another key in another table, so for example you can have something like this :
table USERS
id name info_id
-------------------------------------------------------
1 user1 4
table INFO
id info
--------------
4 someinfo
With a JOIN you can get for example a resultset with the "linked" fields :
SELECT u.name AS name, i.info AS info
FROM USERS u
JOIN INFO i ON u.info_id = i.id
MySql is a so called relational database and having relations (links) between tables is one of the key concepts. In your specific case the "link" you want is called a Foreign Key. You might want to have a read here (there are many more articles around if you have a look on google).
You can retrieve linked records via a JOIN operation as the other answerers have already told you.