mysql left join exists - mysql

Tables:
users: id, name
usersFacebookFriends: id, uid, friendUid, name
I need a query to search for friends (name), and check if they exists in the users table, so then they are also on the website registered.
Is this possible with a left join, and if so how?
Thanks.

SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
LEFT JOIN users u
ON ff.name = u.name;
If you want only those that exist:
SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
INNER JOIN users u
ON ff.name = u.name;
If you want only those that don't exist:
SELECT ff.id, ff.uid, ff.friendUid, ff.name, u.id, u.name
FROM usersFacebookFriends ff
LEFT JOIN users u
ON ff.name = u.name
WHERE u.id IS NULL;

I guess this should make a job. If facebook friend is not registered with your site corrsponding joined User table fields will be NULL.
SELECT f.name, u.* FROM usersFacebookFriends AS f
LEFT JOIN users AS u
ON f.friendUid = u.id
WHERE f.uid = 123
Change LEFT JOIN to INNER JOIN if you need to find only those who exist.

Related

SQL LEFT JOIN WHERE

I have two tables, one with settingIDs and values of those settings for each userID and another with userIDs, their emails and user names.
I am trying to join the values of specific settings, the problem is that not all users have this specific setting tied to their ID, so I end up with less rows than I actually need.
Table 1
userID settingID settingValue
Table 2
userID userDOB userEmail userName
My query looks like this:
SELECT u.userID, u.userEmail, s.settingValue
FROM users u
LEFT JOIN userSettings s ON u.userID = s.userID
WHERE s.settingID = 1
What do I need to do to get all of the users in the list?
Your where clause turns you left join into an inner join. Put the condition in the JOIN
SELECT u.userID, u.userEmail, s.settingValue
FROM users u
LEFT JOIN userSettings s ON u.userID = s.userID
AND s.settingID = 1
you can use UNION ALL clause to combine two JOINS
SELECT u.userID, u.userEmail, s.settingValue
FROM users u
LEFT userSettings s
ON u.userID = s.userID
UNION ALL
SELECT u.userID, u.userEmail, s.settingValue
FROM users u
RIGHT userSettings s
ON u.userID = s.userID

MySql join with 2 Tables and 2 join

I have 2 tables:
posts: userid, lastuserid
users: id, name
I need to join posts.userid = users.id and posts.lastuserid = users.id to get username and lastusername.
My query did as below:
SELECT posts. * , users.name, vUsers.name
FROM posts
INNER JOIN users ON users.id = posts.userid
INNER JOIN Users ON vUsers.id = posts.lastuserid
Is there any other (better) way to do this?
Your query is probably correct. I would encourage you to use table aliases that are abbreviations for the things you are looking for:
SELECT p. * , u.name as username, l.name as lastusername
FROM posts p INNER JOIN
users u
ON u.id = p.userid INNER JOIN
users l
ON l.id = p.lastuserid;
Your query has something called vUsers, which is not defined.

SQL join count(*) of one table to another table as aliases

I have two table, users and comments.
In the users table, there're columns id and username.
In the comments table I have user_id and his message.
And I wanted to create a table that select the username and his comment count when I search a particular username.
How do I write this?
my testing attempt:
SELECT COUNT(*) AS comment_count
FROM song_comments
RIGHT JOIN users
WHERE user_id = 7 AND comments.user_id = users.id
Try this:
SELECT U.Username, COUNT(SC.message) AS comment_count
FROM song_comments SC JOIN
users U ON U.id=SC.user_id
WHERE U.user_id = 7
GROUP BY U.Username
This gives you users and count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
group by u.username
You can add a where to get one user's count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
where u.username = 'Hogan'
group by u.username

mysql query returns only one results from my database

I have 2 tables in my database. Table users and table profile.
users(user_id, surname, email)
profile(profile_id, country, user_id)
user_id in table profile, is FK (comes from users table). I have the following query in order to select all surnames "smith" from my database, that are from country "USA". This is my query:
SELECT u.name,
u.surname,
u.email,
u.user_id,
p.user_id
FROM users u
INNER JOIN profile p ON p.country = 'USA'
WHERE u.surname = 'smith' AND u.user_id = p.user_id
this query works fine, but the problem is that returns only 1 result and not all results from my database (people with surname smith that are from USA). Any idea where it might be the wrong and how to correct it?
You should put the u.user_id = p.user_id condition with ON condition because you want to apply JOIN on 'user_id' field. And where clause should have the remaining condition.
SELECT u.name,
u.surname,
u.email,
u.user_id,
p.user_id
FROM users u
INNER JOIN profile p ON u.user_id = p.user_id
WHERE u.surname = 'smith' And p.country = 'USA'
You can read about INNER JOINS
Use only the columns in your join on condition that are relevant for the join. The columns that link 2 tables together. And put the rest in the where clause. That should work:
SELECT u.name,
u.surname,
u.email,
u.user_id,
p.user_id
FROM users u
INNER JOIN profile p ON u.user_id = p.user_id
WHERE p.country = 'USA'
AND u.surname = 'smith'
This is also better for readability. You can read it like this:
SELECT these columns from table users. JOIN also table profile ON this 2 columns. After that filter the data and return only the records WHERE these conditions are met.

Select only if empty

I have a table of users and a table of things they have on a list -
to show all users who have items on a list I can join the two tables users and user_lists on user_id
e.g.
select u.emailaddr, u.name from users u
join user_lists uw where u.user_id=uw.user_id
group by u.name
QUESTION: How do I show all users who DO NOT have items on a list - to say it another way, I need a list of users who do not have entries in table user_lists
I tried this but it ran endlessly
select u.emailaddr, u.name from users u
join user_lists uw where u.user_id<>uw.user_id
group by u.name
Use LEFT JOIN with IS NULL predicate:
select u.emailaddr, u.name
from users u
LEFT join user_lists uw ON u.user_id = uw.user_id
WHERE uw.user_id IS NULL;
Or: The NOT IN predicate:
select u.emailaddr, u.name
from users u
WHERE u.user_id NOT IN (SELECT user_id
FROM user_lists);
SELECT u.user_id FROM users u
EXCEPT
SELECT uw.user_id FROM user_lists uw
it will give you the ids that exist in users and don't exist in userlists.