sql:检查表B中是否存在表A中的条目

问题描述:

我有一个定义表,我知道它维护得不太好,我们称之为table A.我有另一个表(称为table B),该表要小得多,理想情况下应该是subset of table A,但我知道table A有点陈旧,并且不包含Table B中的新条目.

I have a definition table that I know is not being maintained very well, lets call this table A. I have another table (call it table B) that is much smaller and ideally should be a subset of table A but I know that table A is somewhat stale and does not contain new entries that are in Table B.

请注意,表A和B的列不同.

Note, that tables A and B have different columns.

Table A:
ID, Name, blah, blah, blah, blah

Table B:
ID, Name

我想要表B中的所有行,以使表B中的ID在表A中不存在.我只希望表B中的ID在表A中根本不存在的行.

I want all rows in Table B such that the ID in Table B does NOT exist in Table A. This does not just match rows in Table A; I want only rows in Table B where the ID does NOT exist at all in Table A.

SELECT *
FROM   B
WHERE  NOT EXISTS (SELECT 1 
                   FROM   A 
                   WHERE  A.ID = B.ID)