当前位置: 代码迷 >> Sql Server >> 关于删除表里的重复字段的有关问题,
  详细解决方案

关于删除表里的重复字段的有关问题,

热度:165   发布时间:2016-04-27 19:26:59.0
关于删除表里的重复字段的问题,急!!!!!!!!!!
有两张相关联的表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 Awhere id not in (select ID from (select productid,min(id) as ID from A group by productid) T )  goDelete  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 taFOR DELETEAS    DELETE FROM tb b    INNER JON DELETED a        ON a.id=b.id
------解决方案--------------------
---触发器
SQL code
create trigger del_b on 表1for deleteasdelete 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 )
  相关解决方案