首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-10 
关于删除表里的重复字段的问题,急!!!!!!!!!!有两张相关联的表a,ba表idnamenumberproductid3421620293022.4

关于删除表里的重复字段的问题,急!!!!!!!!!!
有两张相关联的表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 )

热点排行