Home > Mobile >  Database join where
Database join where

Time:02-04

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

CodePudding user response:

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.

CodePudding user response:

Please try this,

Example 1:

SELECT
U.ID,U.LASTNAME,U.FIRSTNAME 
FROM 
USERS U
LEFT JOIN 
GROUP_ITEMS G
ON U.ID=G.STUDENT_ID
WHERE G.GROUP_ID <>55

Example 2:

SELECT
U.ID,U.LASTNAME,U.FIRSTNAME  
FROM 
USERS U
LEFT JOIN 
GROUP_ITEMS G
ON U.ID=G.STUDENT_ID
WHERE G.GROUP_ID <>11

CodePudding user response:

You can use join statements in SQL

For Example for return 1:

SELECT * FROM users u left join group_items gi on gi.student_id = u.id

this query will return all students with their group_id for filter by group you can use where statement.

SELECT * FROM users u left join group_items gi on gi.student_id = u.id where gi.group_id<>11

  •  Tags:  
  • Related