Skip to content
Advertisement

Database join where

Database

Users

id lastname firstname
1 Sardor Sattarov
2 Nurmuhammad To’xtayev
3 Jasur Sattarov

Group_items

id student_id group_id
1 2 55
2 1 55
3 2 11

Return example 1

condition

users.id == group_items.student_id do not publish a table that satisfies this desire group_items.id == 55

id lastname firstname
3 Jasur Sattarov

example 2

condition

users.id == group_items.student_id do not publish a table that satisfies this desire group_items.id == 11

id lastname firstname
1 Sardor Sattarov
3 Jasur Sattarov

Advertisement

Answer

Looks like you want OR instead of AND.

SELECT
    * 
FROM
    users u
    LEFT JOIN group_items gi ON u.id = gi.student_id 
WHERE
    gi.student_id IS NULL 
    OR gi.group_id <> 5

Studends without a group plus studends in all groups but 5.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement