mysql查询筛选重复数据sql语句
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-28 21:40:20 浏览: 评论:0
以前讲过大量的重复数据过滤语句,下面小编来给大家介绍一些自己收藏了查询筛选重复数据sql语句,希望对各位朋友有所帮助.
查询重复数据数量,代码如下:
select device_id from device group by device_id having count(device_id) > 1;
查询所有重复数据,代码如下:
- select userid, device_id, create_date from device where device_id in (select device_id from device group by device_id having count(device_id) > 1) order by device_id,create_date desc ;
重复一条中create_date 最新的那一条,代码如下:
select max(create_date) from device group by device_id having count(device_id)>1;
筛选查询,代码如下:
- select * from device where device_id in (select device_id from device group by device_id having count(device_id) > 1) and create_date not in (select max(create_date) from device group by device_id having count(device_id)>1) order by device_id,create_date desc ;
下面再看一些实例吧.
表结构如下,代码如下:
- mysql> desc test1;
- +--------------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------------+------------------+------+-----+---------+----------------+
- | ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | SENDERNAME | varchar(32) | YES | | NULL | |
- | RECEIVERNAME | varchar(64) | YES | | NULL | |
- | SENDER | varchar(64) | NO | | NULL | |
- | RECEIVER | varchar(64) | NO | | NULL | |
- | SUBJECT | varchar(512) | NO | | NULL | |
- | CONTENT | text | NO | | NULL | |
- | PRIORITY | int(11) | NO | MUL | NULL | |
- | STATUS | int(11) | NO | MUL | NULL | |
- | CREATETIME | datetime | NO | | NULL | |
- | SENDTIME | datetime | YES | | NULL | |
- +--------------+------------------+------+-----+---------+----------------+
subject和RECEIVER,需要做uniq key,但设计时未做,,后面的数据就有很多重复的记录.
1.查询需要删除的记录,会保留一条记录,代码如下:
- select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b on a.id< b.bid where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid
2.删除重复记录,只保留一条记录,注意,subject,RECEIVER 要索引,否则会很慢的,代码如下:
- delete a from test1 a, (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid; //phpfensi.com
好了筛选重复数据的sql语句大概就这些了,如果你能理解那几乎不担心重复数据这一说了.
Tags: mysql查询筛选 mysql重复数据
相关文章
- ·MySQL处理重复 防止重复数据实例(2014-09-29)
- ·mysql删除字段重复的数据sql语句(2014-10-01)
- ·mysql删除重复数据记录sql语句总结(2014-10-05)
- ·MYSQL中GROUP分组去除重复数据(2015-04-17)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)