简体   繁体   中英

How to Select data from two tables which are not identical on another table using WHERE condition in MySQL

I have two tables, those are " Student " and " User ". I want to select the first_name and Last_name from "Student" table which does not match with "Student_ID" of the user table.

SELECT student.student_id, 
       student.first_name, 
       student.last_name 
FROM   student, 
       USER 
WHERE  student.student_id != USER.student_id 

Same as NOT IN version but perform a little bit better. According to this: https://www.eversql.com/sql-syntax-check-validator/

SELECT
  student.student_id,
  student.first_name,
  student.last_name
FROM
  student
WHERE
  NOT EXISTS (
    SELECT
      student_id
    from
      user
    where
      user.student_id = student.student_id
  )

Try using this query:

SELECT firstname,lastname 
FROM Student 
WHERE ID NOT IN(SELECT Student_ID from User);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM