建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 数据库 > Mysql 教程 >

如何用sql语句查询和删除表中重复数据

时间:2011-03-08 09:13来源: 作者: 点击:
如何用sql语句查询和删除表中重复数据 1、查询表中重复数据(单字段) Select * From 表 Where 字段1 In (Select 字段1 From 表 Group By 字段1 Having Count(字段1) > 1)   2、删除表中多余的重复记录,只留有rowid最小的记录(单字段) Delete From 表 Where 字

如何用sql语句查询和删除表中重复数据

1、查询表中重复数据(单字段)
Select * From 表 Where 字段1 In (Select 字段1 From 表 Group By 字段1 Having Count(字段1) > 1)

 

2、删除表中多余的重复记录,只留有rowid最小的记录(单字段)
Delete From 表
Where 字段1 In (Select 字段1 From 表 Group By 字段1 Having Count(字段1) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1 Having Count(字段1) > 1)
     

3、查找表中多余的重复记录(多个字段)
Select * From 表 a Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段2 Having Count(*) > 1)

4、删除表中多余的重复记录,只留有rowid最小的记录(多个字段)
Delete From 表 a
Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段2 Having Count(*) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1, 字段2 Having Count(*) > 1)
     
5.删除多于的重复记录(单个字段,多个字段)
delete from table where id not in ( select min(id) from table group by name)
或者
delete from table where id not in ( select min(id) from table group by 字段1,字段2)

6.删除多余的重复记录(单个字段,多个字段)
delete from table where id in ( select max(id) from table group by name having count(*)>1)
 

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
Select *
From 表 a
Where (a.字段1, a.字段2) In (Select 字段1, 字段2 From 表 Group By 字段1, 字段2 Having Count(*) > 1) And
   Rowid Not In (Select Min(Rowid) From 表 Group By 字段1, 字段2 Having Count(*) > 1)


查单个字段的重复次数
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片