简单的数据库语句

来源:百度知道 编辑:UC知道 时间:2024/05/21 01:02:04
怎么在第一张表中查出第二张表里没有的记录
比如我一张表中有10条记录 就如 ID为 1 2 3 4 5 6 7 8 9 10 这10条记录
第二表中ID 有 1 2 3 4 5 只有5条记录
怎么查出表二中没有 而表一中有的记录呢

select * from table1 where ID not in (select ID from table2 where ....)

select * from table1 where ID not in (select ID from table2)

select * from table1 where ID not in (select ID from table2)
这个很OK

用一个嵌套查询建立两个表之间的关系
查询语句就如上面几位朋友所说的:
select * from table1 where ID not in (select ID from table2)
先从table2中找出table2中所有的记录,在从table1中找出table1中所有的记录,然后通过"not in "比较出table2中没有而table1中有的记录.

select * from table1 where ID not in (select ID from table2)

就是上面所说的
select * from table1 where table.id not in(select table2.id from table2 )