日期:2014-05-18  浏览次数:20472 次

关于删除表里的重复字段的问题,急!!!!!!!!!!
有两张相关联的表a,b
a表 
id name number productid
342 162029302 2.4*1217 443
321 163229302 4.9*1219 443
765 322029303 8.2*1519 577
864 452029311 6.3*1267 443

b表 
id name number  
342 162029302 2.4*1217  
321 163229302 4.9*1219  
765 322029303 8.2*1519  
864 452029311 6.3*1267  
   
现在要删除a表productid重复的字段,只保留一条(任意)。同时要删除b表与a表关联的对应记录。如何实现 ????


------解决方案--------------------
SQL code

Delete 
from A
where id not in (select ID from (select productid,min(id) as ID from A group by productid) T )  
go

Delete  
from b 
where id not in (select iD from A)

------解决方案--------------------
SQL code

create table # (id varchar(20),name varchar(20),number varchar(20),productid varchar(20) )

insert into # values('342','162029302','2.4*1217','443')

insert into # values('321','163229302','4.9*1219','443')

insert into # values('765',' price th

------解决方案--------------------
SQL code
delete aa from a aa where exists(select 1 from a where  productid=aa.productid and id>aa.id)
delete b where id not in(select id from a)

------解决方案--------------------
ta上创建触发器


SQL code
CREATE TRIGGER t ON ta
FOR DELETE
AS
    DELETE FROM tb b
    INNER JON DELETED a
        ON a.id=b.id

------解决方案--------------------
---触发器
SQL code

create trigger del_b on 表1
for delete
as
delete from # where id in (select id from deleted)

------解决方案--------------------
delete from b
where b.id in ( select max(id) from a group by productid having count(1) > 1 )


delete from a
where a.id in ( select max(id) from a group by productid having count(1) > 1 )