Can such SELECTion be made? Based only on QTZ_USERS UUID can I "foreach" all memberships with GROUP_ID's to find the GROUP_ID with the highest prority?
How can I approach that?
Assuming you want details from all the tables then join them together and to a sub query to get the latest priority for each user.
Something like the following (although select the actual columns you want rather than use SELECT *).
SELECT *
FROM QTZ_USERS u
INNER JOIN MEMBERSHIPS m
ON u.UUID = m.UUID
INNER JOIN GROUPS g
ON m.GROUP_ID = g.GROUP_ID
INNER JOIN
(
SELECT m.UUID, MAX(g.PRIORITY) AS max_priority
FROM MEMBERSHIPS m
INNER JOIN GROUPS g
ON m.GROUP_ID = g.GROUP_ID
GROUP BY m.UUID
) sub0
ON m.UUID = sub0.UUID
AND g.PRIORITY = sub0.max_priority
Principially you do this with GROUP BY and aggregate functions, here MAX.
SELECT
m.UUID,
MAX(g.PRIORITY)
FROM
memberships m
INNER JOIN
groups g
ON
m.GROUP_ID = g.GROUP_ID
GROUP BY
m.UUID;
If you need the GROUP_ID, then you can use a subselect
SELECT
m.UUID,
g.GROUP_ID
FROM
memberships m
INNER JOIN
groups g
ON
m.group_id = g.group_id
WHERE
g.priority = (
SELECT
MAX(g2.priority)
FROM
groups g2
WHERE
g.group_id = g2.group_id
GROUP BY
g2.group_id
);
Related
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';
SELECT groups.*
, roles.rol_display_name
, users.usr_fname
FROM groups, groups_roles, roles, users
WHERE groups.id = groups_roles.groups_id and
roles.id = groups_roles.roles_id and
groups.grp_manager_id = users.id
In my query if all the AND conditions are true then only it will show all the records but my requirement is even if manager.id is null it should show all the records with users.usr_fname as null.
Use left join instead of inner join (implicit or otherwise) when you join users table:
SELECT g.*
, r.rol_display_name
, u.usr_fname
FROM groups g
JOIN groups_roles gr on g.id = gr.groups_id
JOIN roles r on r.id = gr.roles_id
LEFT JOIN users u on g.grp_manager_id = u.id
I have a database with two tables groups and members. The columns from these tables are below.
Groups table:
group_id,group_name
Members table:
member_id,group_id,status
I have an app that needs to display a list of groups along with the groups members. What is the most efficient way to say SELECT * from groups then for each group/row returned do another select on the members table to query the members (for example SELECT * FROM members WHERE group_id=X).
select
g.group_id,
g.group_name,
m.member_id,
m.status
from groups g
inner join members m on g.group_id = m.group_id
Assuming that both members and groups have a column named group_id, you can use join to fetch the results you want:
select m.*
from members as m
inner join groups as g on m.group_id = g.group_id
where group_id = X
SELECT *
FROM groups g
LEFT JOIN members m
ON g.group_id = m.group_id
This would return all groups and their members.
You can introduce variables, so for example
SET #groups = (SELECT TOP 1 group_id FROM groups)
and add it to your query.
SELECT *
FROM groups g
LEFT JOIN members m
ON g.group_id = m.group_id
WHERE g.group_id = #groups
I'm having some difficulty with my JOIN and IS NULL..
Basically what I want to do is find all members who are not apart of certain group category ID's that I specify.
SELECT m.* FROM elvanto_members AS m
LEFT JOIN elvanto_groups AS g ON g.deleted = 0
LEFT JOIN elvanto_groups_categories AS gc ON gc.group_id = g.id AND (gc.category_id = '1' OR gc.category_id = '2')
WHERE gr.id IS NULL
Some members aren't apart of any group categories at all which is why I've made it a LEFT JOIN.
Am I making sense? Do you have any idea how to fix this?
Try this:
SELECT m.* FROM elvanto_members AS m
WHERE
not exists
(
SELECT 1 FROM
elvanto_groups AS g
INNER JOIN elvanto_groups_categories AS gc ON
gc.group_id = g.id
WHERE
gc.category_id IN ('1','2') AND
g.id = m.group_id AND
g.deleted = 0
)
SELECT m.*
FROM elvanto_members AS m
LEFT JOIN elvanto_groups AS g ON g.id = m.group_id AND g.deleted = 0
LEFT JOIN elvanto_groups_categories AS gc ON gc.group_id = g.id
AND gc.category_id IN ('1','2')
WHERE gc.group_id IS NULL
GROUP BY m.*
I filled in on spec what was missing in the question.
Is category_id really a string type? I would expect it to be numeric. Then this expression should be:
AND gc.category_id IN (1,2)
I have following issue. There are two tables - groups and students and third pivot table group_student.
Query to get students from specific groups (id:1,8) is clear...
SELECT DISTINCT s.*
FROM students AS s
Inner Join group_student AS gs ON gs.student_id = s.id
Inner Join groups AS g ON g.id = gs.group_id
WHERE g.id IN ("1","8")
It works. But, how to query, if I want to select just segment of students of group id 1. For example s.name = "john". So the result should be: all students from group id 8 + all students with name "john" from group id 1.
Thanx for posts :-)
Try this:
SELECT DISTINCT s.*
FROM students AS s
Inner Join group_student AS gs ON gs.student_id = s.id
Inner Join groups AS g ON g.id = gs.group_id
WHERE g.id ="8" or (g.id="1" and s.name = "john")
you can use UNION too
SELECT DISTINCT s.*
FROM students AS s
Inner Join group_student AS gs ON gs.student_id = s.id
Inner Join groups AS g ON g.id = gs.group_id
WHERE g.id = 8
UNION
SELECT DISTINCT s.*
FROM students AS s
Inner Join group_student AS gs ON gs.student_id = s.id
Inner Join groups AS g ON g.id = gs.group_id
WHERE gp.id="1" and s.name = "john"
One more option, avoiding DISTINCT and the (probably unneeded) join to groups:
SELECT s.*
FROM students AS s
WHERE EXISTS
( SELECT *
FROM group_student AS gs
WHERE gs.student_id = s.id
AND ( gs.group_id = 8
OR (gs.group_id, s.name) = (1, 'john')
)
)