简体   繁体   中英

find difference between two tables in sql

what is the equivalent sql of this table? i want to get only the names of the persons from all_user table who are not in your_friend table


Angelina Jolie
Brad Pitt
Peter Parker
Clark Kent


table name: all_user
(users)
Angelina Jolie
Brad Pitt
Peter Parker
Mary Jane
Clark Kent
Lois Lane


table name: your_friend
(friend)
Lois Lane
Marj Jane


select distinct * from all_user where not in (select * from your_friend where all_user.users = your_friend.friend)

This is my answer and i am getting an error near IN syntax.

You haven't specified a RDBMS. If your engine supports it the EXCEPT operation is what you are looking for. It evaluates the output of two query expressions and returns the difference between the results. The result set contains all rows returned from the first query expression except those rows that are also returned from the second query expression.

SELECT DISTINCT <Columns_To_Be_Included>
  FROM all_user 
EXCEPT
SELECT DISTINCT <Columns_To_Be_Included>
  FROM your_friend

But be careful that this works at the record level. So, you have to only specify the columns that you want to include in the comparison.

尝试这个

select distinct * from all_users where users not in (select distinct friend from your_friend)

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