Select only if empty - mysql

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.

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

select from 3 tables with null values

I have 3 tables
Users
COLUMNS User Name, USER ID
Groups
COLUMNS Group Name,Group Id
Users_Group
COLUMNS User ID, Group ID
the User group contains the relation between Users and groups.
I want to select from users and groups to get user names and group names togother.
But I want also to get the users who dont have group where the return value of group name will be null
How to create such SQL in mysql
You need to left join as
select
u.username,
g.group_name
from users u
left join users_group ug on ug.user_id = u.user_id
left join groups g on g.group_id = ug.group_id
select u.username,
g.groupname
from users u
left join user_group ug on u.userid=ug.userid
left join groups g on g.groupid=ug.groupid

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 left join exists

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.

Why doesn't my query return any results?

Why does this sql query only show results if they only have a row in users_warnings?
SELECT
u.id,
uw.warning
FROM
users u
INNER JOIN users_warnings uw ON (
u.id = uw.uID
)
LIMIT 21
I wish to show all users, but also grab the column "warning" in users_warnings, if any.
An INNER JOIN only returns something if there is data in both tables.
Try a LEFT JOIN instead:
SELECT u.id, uw.warning FROM users u
LEFT JOIN users_warnings uw ON (u.id = uw.uID)
LIMIT 21
Because you're using an inner join, which requires a row to exist on the joined table. Try the following:
SELECT
u.id,
uw.warning
FROM
users u
LEFT JOIN users_warnings uw ON (
u.id = uw.uID
)
LIMIT 21
Change your inner join for a left join, as so:
SELECT u.id, uw.warning FROM users u
LEFT JOIN users_warnings uw ON (u.id = uw.uID)
LIMIT 21
SELECT
u.id,
IFNULL(uw.warning,'') warning
FROM
(SELECT id FROM users LIMIT 21) u
LEFT JOIN users_warnings uw
ON u.id = uw.uID
;
Should be a LEFT JOIN not INNER JOIN
Refactored the query to get first 21 users before attempting a JOIN (faster query)
Defaulted warning to blank string if user had no warning