SQL query: data that exists in one table but not in another
Related link
https://www.suibibk.com/topic/905203656905719808/
Method 1
Use not in, easy to understand, low efficiency
SELECT * FROM A WHERE A.a_id NOT IN (SELECT b_id FROM B)
Method 2
Use left join…on… , “B.b_id IS NULL” indicates the records whose B.b_id field is NULL after the left join
SELECT * FROM A LEFT JOIN B ON A.a_id=B.b_id WHERE B.b_id IS NULL
Method 3
The logic is relatively complicated, but the fastest
is the query statement after WHERE, query according to the id:
if there is in A, there is also in B, it is true, return 1, if 1=0 is not true, it will not be output
If there is in A, If it is not in B, it will be false, return 0, and if 0=0 is established, it will be output,
that is, the data that exists in A but not in B can be obtained
SELECT * FROM A WHERE (SELECT COUNT(1) AS num FROM B WHERE A.a_id=B.b_id)=0
According to my current use, when there are more than three tables, the second one is the best to understand and use.