同时去除两张表里的重复记录的SQL语句怎么写?
两表关联:表 A 的字段 infoID = 表B的字段 infoID
谢谢!
问题点数:50、回复次数:6Top
1 楼itblog(Just for wife!)回复于 2006-06-04 08:16:11 得分 5
表结构贴出来~Top
2 楼hanbaojun(闲庭信步)回复于 2006-06-04 08:18:42 得分 0
A:
infoID,className,title
B:
infoID,content
A.infoID = B.infoID
A表 className,title 这两个字段不能重复,如果重复 即 B表中也有重复记录
需要一条同时删除两表里的重复记录Top
3 楼itblog(Just for wife!)回复于 2006-06-04 08:32:18 得分 20
这是两个基本的删除语句~
delete A1 from A A1 where exists(select 1 from a where infoid>a1.infoid and classname=a1.classname and title=a1.title)
delete B1 from B b1 where exists(select 1 from B where infoid>b1.infoid and content=b1.content)Top
4 楼itblog(Just for wife!)回复于 2006-06-04 08:34:05 得分 20
你可以把它变成存储过程,调用存储过程就可以了~
CREATE PROC SP_DELETE
AS
delete A1 from A A1 where exists(select 1 from a where infoid>a1.infoid and classname=a1.classname and title=a1.title)
delete B1 from B b1 where exists(select 1 from B where infoid>b1.infoid and content=b1.content)
END
GO
EXEC SP_DELETETop
5 楼wangtiecheng(不知不为过,不学就是错!)回复于 2006-06-04 10:14:46 得分 5
一条语句无法实现,至少用2条SQL语句。
可以用存储过程,添加事务,保证数据处理的完整性。Top
6 楼losedxyz(我真的一无所有)回复于 2006-06-04 17:18:27 得分 0
markTop




