+ -
当前位置:首页 → 问答吧 → 删除重复记录

删除重复记录

时间:2011-11-11

来源:互联网

在数据库里面有两张表:

A表:
xhid clothingid nums
1 822001Ai2 1

B表:
clothingid nums
822001Ai2 1

现在的情况是这样,将A表里面,clothingid在B表中存的,并且A表中存在多条记录的删除,
语句要怎么写;

比如:
A表中的clothingid,有3个记录都是822001Ai2,B表里面也存在着clothingid为822001Ai2的记录,
现在要把A表中clothingid为822001Ai2的记录删除到剩下一条;

不知语句应如何写;
请高手帮忙;

作者: wuzhj3107   发布时间: 2011-11-11

SQL code
delete 
 t
from 
 a t
where
 exists(select 1 from b where clothingid=t.clothingid)
and
 exists(select 1 from a where clothingid=t.clothingid and xhid<t.xhid)

作者: fredrickhu   发布时间: 2011-11-11

delete a
from A t where exists(select 1 from A where clothingid=t.clothingid and xhid >t.xhid)
 and exists(select 1 from B where clothingid=t.clothingid)

作者: ssp2009   发布时间: 2011-11-11

SQL code
delete t1
from A as t1 
inner join B as on t1.clothingid=b.clothingid
where t1.xhid not in(select 1 from A where clothingid=t1.clothingid)

作者: roy_88   发布时间: 2011-11-11

可能我说的不是很明白,A表中的xhid不是主键,此字段的值是允许重复的,
xhid+clothingid不可以出现重复;

作者: wuzhj3107   发布时间: 2011-11-11