select from 3 tables with null values - mysql

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

Related

SQL: query get all users for a specific group and each user with all groups

I have 3 tables: USERS, GROUPS, GROUP_USERS
I need to get a list of users that are in a specific group and all users needs to contain all groups that are members of.
I tried something like this:
select group_concat(g.name) as groups
, u.*
from USERS u
JOIN GROUP_USERS gu
ON gu.USER_ID = u.ID
JOIN GROUPS g
ON g.ID = ug.GROUP_ID
WHERE ug.GROUP_ID = SOMEID
GROUP
BY u.ID
But this will return all users in group with id SOMEID and will concat only that one group that was use for search, even if a users is member of multiple groups.
Thanks.
I see. You want all groups for a subset of users that are in one group. That would be:
SELECT u.*, group_concat(g.name) as groups
FROM USERS u JOIN
GROUP_USERS gu
ON gu.USER_ID = u.ID JOIN
GROUPS g
ON g.ID = gu.GROUP_ID
GROUP BY u.ID
HAVING SUM(g.id = ?) > 0;
In other words, you need to move the filtering to the HAVING clause.

Getting all records in many-to-many relationship even if the relationship is missing

I have a user table, group table, and user_group table for many to many.
I am confused on how I can get the list of all users belonging to a specific group and if the user does not belong to the current group, I still want his record listed with the group field set to NULL.
If I understand correctly, you can do:
select u.*, ug.group_id
from users u left join
user_group ug
on ug.user_id = u.user_id and ug.group_id = <the group>;
If you do not care about including a group without users, this should work; all groupless users should be collected together under the NULL group_id's userIDs.
SELECT g.group_id, GROUP_CONCAT(u.user_id) AS userIDs
FROM `users` AS u
LEFT JOIN `user_group` AS ug ON u.user_id = ug.user_id
LEFT JOIN `group` AS g ON ug.group_id = g.group_id
GROUP BY g.group_id
;

Fetching data in case of one -to- many relationship in MySQL

I have 3 tables users, user_group, and groups.
users have one to many relationship with groups.
If I want to fetch only those users who don't have group Mathematics.
I have using the following query for this purpose:
SELECT * FROM users
INNER JOIN user_group ON user_group.user_id = user.UserID
INNER JOIN groups ON user_group.group_id = groups.group_id
WHERE groups.Name <> 'Mathematics';
But it is returning multiple records against all Users. Suppose, if I have user John and he joined 3 groups Science, Mathematics and English. In this case, it will return two records of user John. I want to remove user John totally from the list.
You can use NOT EXISTS:
SELECT *
FROM users AS u
WHERE NOT EXISTS (SELECT 1
FROM user_group AS ug
INNER JOIN groups AS g ON ug.group_id = g.group_id
WHERE ug.user_id = u.UserID AND g.Name = 'Mathematics');
Demo here
If you want to do it using joins, then this is a way:
SELECT u.*
FROM users AS u
LEFT JOIN (
SELECT user_id
FROM user_group
INNER JOIN groups
ON user_group.group_id = groups.group_id AND groups.Name = 'Mathematics'
) AS g ON u.UserID = g.user_id
WHERE g.user_id IS NULL
Demo here
SELECT * FROM users
LEFT JOIN user_group ON user.UserID = user_group.user_id
LEFT JOIN groups ON user_group.group_id = groups.group_id
WHERE groups.Name != 'Mathematics';

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

Select Two Values Based Off Same Column

I am trying to select two usernames in one row of a query one would be called 'username' and the second one say 'username2'. This is the code I have atm. The users table uses a primary key of user_id.
SELECT
r.report_id,
r.poster_id,
r.reporter_id,
u.username,
(2nd username)
FROM reports r,
users u
WHERE r.report_id = :report_id
AND r.reporter_id = u.user_id
AND r.poster_id = u.user_id
you need to join the table user twice
SELECT
r.report_id,
r.poster_id,
r.reporter_id,
u.username AS ReporterName,
b.userName as PosterName
FROM
reports r
INNER JOIN users u
ON r.reporter_id=u.user_id
INNER JOIN users b
ON r.poster_id=b.user_id
WHERE
r.report_id=:report_id
Here is the code for MySQL
SELECT
r.report_id,
r.poster_id,
r.reporter_id,
u.username,
u.username username2,
FROM reports r,
users u
WHERE r.report_id = :report_id
AND r.reporter_id = u.user_id
AND r.poster_id = u.user_id