主题:SQL查询求助。。。。
yekanet
[专家分:0] 发布于 2007-04-19 02:13:00
在SQL里有两个表,两个表都有相同的字段,(如:姓名,年龄,性别),通过怎样的查询得到表1中的记录,而这些记录在表2中不存在(即在表1中查找表2中不存在的记录)
回复列表 (共4个回复)
沙发
菜鸭 [专家分:5120] 发布于 2007-04-19 08:12:00
select * from 表1 where id not in (select id from 表2)
板凳
Haggard_doom [专家分:80] 发布于 2007-04-19 18:40:00
[quote]select * from 表1 where id not in (select id from 表2)[/quote]
学习~!
3 楼
潇洒老乌龟 [专家分:1050] 发布于 2007-04-20 11:13:00
姓名,年龄,性别
1、姓名是主键
select * from tb1 where 姓名 not in (select 姓名 from tb2)
2、姓名,年龄,性别
select * from tb1 where cast(姓名 as varchar) + cast(年龄 as varchar) + cast(性别 as varchar) not in (select cast(姓名 as varchar) + cast(年龄 as varchar) + cast(性别 as varchar) from tb2)
3、如果是查全表不重复。
使用checksum()
4 楼
中国台湾 [专家分:2140] 发布于 2007-05-02 00:18:00
I have two tables( a & b). How do i get records from table a which don't match with the records in table b
Depending on where it needs to be used...
SELECT a.*
FROM TableA a
LEFT OUTER JOIN
TableB b
ON a.somecol = b.somecol
WHERE b.somecol IS NULL
...OR...
SELECT *
FROM TableA
WHERE somecol NOT IN (SELECT somecol FROM TableB)
...OR...
SELECT a.*
FROM TableA a
WHERE NOT EXISTS (SELECT 1 FROM TableB b WHERE b.somecol = a.somecol)
我来回复