情况一,直接查出重复
--查出表中有重复的id的记录,并计算相同id的数量
select id,count(id) from @table group by id having(count(id)>1)
其中,group by id,是按id字段分组查询:
select id,count(id) from @table group by id
可以得到各不同id的数量合计
having(count(id)>1)判断数量大于1,也就是有重复id的记录
情况二,按照重复出现的次数次数排序
利用Mysql中的 的聚合函数 count(*) 可以实现这个功能,例如需要查询data表中name出现次数最多的记录,可以先按照group by name分组,用count算出分组里的条数,再按照count排序:
select name,count(*) from data group by name order by count(*) DESC limit 1
不加limit限制将返回按照name重复次数排列的数据
情况三,如果我们一张表有多个字段重复需要 查询一张表某几个字段 数据量重复次数最多的一条数据
查询eye,nose,mouth三个字段中重复数据最多的,当每个字段的数据没有重复的就查时间上最新的。结果是查出一条数据。(考虑一种情况是,比如eye字段中,a1和a2数量一样多,也是查询时间上最新的一条)
查询结果:
eye nose mouth
a2 b2 c1
a2为eye字段中数量最多(与a1数量相同取时间最新的数据),b2为nose字段中最多的数据,c1为mouth字段中数据最多的
表结构
mysql> select * from face;
+----+-----+------+-------+---------------------+
| id | eye | nose | mouth | create_time |
+----+-----+------+-------+---------------------+
| 1 | a1 | b1 | c1 | 2015-04-07 20:40:11 |
| 2 | a1 | b2 | c3 | 2015-04-07 20:40:28 |
| 3 | a2 | b2 | c1 | 2015-04-07 20:40:52 |
| 4 | a2 | b3 | c2 | 2015-04-07 20:41:03 |
| 5 | a3 | b4 | c4 | 2015-04-07 20:41:19 |
+----+-----+------+-------+---------------------+
5 rows in set (0.00 sec)
mysql> select eye.eye, nose.nose, mouth.mouth from (select eye, create_time, count(*) from face group by eye order by count(*) desc, max(create_time) desc limit 1) as eye, (select nose, create_time, count(*) from face group by nose order by count(*) desc, max(create_time) desc limit 1) as nose, (select mouth, create_time, count(*) from face group by mouth order by count(*) desc, max(create_time) desc limit 1) as mouth;
+-----+------+-------+
| eye | nose | mouth |
+-----+------+-------+
| a2 | b2 | c1 |
+-----+------+-------+
1 row in set (0.01 sec)
看了下一楼回复的子查询,在处理数量一样,选时间最新的上有点小问题。这就是上面我加入max(create_time)的原因。不用max,返回的时间都是第一条记录的。处理不了这种情况:a2第二条记录的时间大于a1两条记录的时间。但是a1第一条记录的时间大于a2第一条记录的时间
注意返回时间:
有max得情况:
mysql> select eye, create_time, count(*) from face group by eye order by count(*) desc, max(create_time) desc limit 1;
+-----+---------------------+----------+
| eye | max(create_time) | count(*) |
+-----+---------------------+----------+
| a2 | 2015-04-07 20:41:03 | 2 |
+-----+---------------------+----------+
1 row in set (0.00 sec)
没有max的情况:
mysql> select eye, create_time, count(*) from face group by eye order by count(*) desc, create_time desc limit 1;
+-----+---------------------+----------+
| eye | create_time | count(*) |
+-----+---------------------+----------+
| a2 | 2015-04-07 20:40:52 | 2 |
+-----+---------------------+----------+
1 row in set (0.00 sec)
注:max放在order by 后面。