百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>数据库> 删除重复数据sql语句
分享文章到:

删除重复数据sql语句

发布时间:01/15 来源: 浏览: 关键词:

删除重复数据sql语句
方法一

假设有重复的字段为name,address,要求得到这两个字段唯一的结果集
select identity(int,1,1) as autoid, * into #tmp from tablename
select min(autoid) as autoid into #tmp2 from #tmp group by name,autoid
select * from #tmp where autoid in(select autoid from #tmp2)

方法二
declare @max integer,@id integer
declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) > 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from 表名 where 主字段 = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0

方法三

如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
select distinct * into #tmp from tablename
drop table tablename
select * into tablename from #tmp
drop table #tmp

实例

vieworder  id号      打卡机     消费类型    时间        价钱
1           a1       1号机     早餐       2006-08-03 07:09:23.000         2
2           a1       1号机     早餐       2006-08-03 07:10:13.000         2
3           a1       1号机     早餐       2006-08-03 07:10:19.000         2
4           a1       1号机     午餐       2006-08-03 12:02:10.000         5
5           a2       1号机     午餐       2006-08-03 12:11:10.000         5
6           a2       1号机     午餐       2006-08-03 12:12:10.000         5

代码

delete from 表 a
where exists(select * from 表 where  消费类型=a.消费类型 and 时间>=dateadd(minute,-2,a.时间) and 时间<a.时间)

删除之前先用select语句查看要被删除的数据
select *
from 表 a
where exists(select * from 表 where  消费类型=a.消费类型 and 时间>=dateadd(minute,-2,a.时间) and 时间<a.时间)

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有2人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板