如何获取mysql中缺少行的表

时间:2010-03-11 12:23:36

标签: mysql

我有两个mysql表

表A

colA1   colA2
1       whatever
2       whatever
3       whatever
4       whatever
5       whatever
6       whatever

第二个表基本上是从tableA派生的,但是删除了一些行

tableB的

colB1    colB2
1       whatever
2       whatever
4       whatever
6       whatever

如何编写查询以从上面两个表中获取缺失行的表

colC1   colC2
3      whatever
5      whatever

3 个答案:

答案 0 :(得分:10)

SELECT t1.*
FROM TableA t1 LEFT JOIN
     TableB t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL

答案 1 :(得分:0)

这样的事情:

select *
from tableA
where not exists (
    select 1
    from tableB
    where tableB.colB1 = tableA.coldA1
)


即,您从tableA中选择了tableB中没有等效数据的数据。

答案 2 :(得分:0)

select * from  tableA where colA1 not in ( select colA1 from tableB   ) ;