sql如何删除某两张表中不相同的的数据,以某一字段为比较

两张表中都有一个字段的内容是对应的,只有有一个表中的数据多点,想把多的都删掉

这样,比如:
少数据的表为:A
多数据的表为:B
你需要的字段:C
select C from A,说明这个是A表里面的C字段
然后B中,你不要超出C的,那么你可以这样
select * from B where C in (select C from A)
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-05-20
declare @a table(id int)

insert into @a
select 1
union select 2
union select 3
union select 4

declare @b table(id int)

insert into @b
select 1
union select 2
union select 6
union select 5

select * from @a a inner join @b b on a.id=b.id

delete from @a where id not in (select id from @b)

delete from @b where id not in (select id from @a)

select * from @a
select * from @b
第2个回答  2011-05-22
这不是都回答了吗,咋看不懂啊。。。
第3个回答  推荐于2018-04-14
依楼上的事例:
DELETE FROM 表B WHERE C not in (SELECT C FROM A)本回答被提问者和网友采纳
相似回答