简体   繁体   中英

How do I select a row from one table where the value row does not exist in another table?

Let's say I have two identical tables, A and B, with the row "x".

I want to select all elements in A, where the value of x in A is not in any value of x of B.

How do I do that?

You could also do something like this:

SELECT * FROM TableA
LEFT JOIN TableB on TableA.X = TableB.X
WHERE TableB.X IS NULL

(For the very straightforward example in your question, a NOT EXISTS / NOT IN approach is probably preferable, but is your real query is more complex, this is an option you might want to consider; if, for instace, you want som information from TableB where there is a match, but also want to know where there isn't one)

I'm having some trouble to understand what you need.
Anyway try this:

SELECT * FROM tableA 
WHERE x not IN (SELECT x FROM tableB)
select *
from TableA
except
select *
from TableB

最快的是左连接

SELECT * FROM A LEFT JOIN B ON A.X = B.X WHERE B.X IS NULL

用它 :

select * from a where x not in (select x from b)

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