首页 > 其他 > 详细

找重复值

时间:2018-01-16 22:45:28      阅读:204      评论:0      收藏:0      [点我收藏+]

方便演示,我们创建一个名为contacts表,其中包含四个列:idfirst_namelast_nameemail

表中数据如下

| id | first_name | last_name       | email                          |
+----+------------+-----------------+--------------------------------+
|  1 | Carine     | Schmitt         | carine.schmitt@qq.com          |
|  2 | Jean       | King            | jean.king@yiibai.com               |
|  3 | Peter      | Ferguson        | peter.ferguson@google.com      |
|  4 | Janine     | Labrune         | janine.labrune@aol.com         |
|  5 | Jonas      | Bergulfsen      | jonas.bergulfsen@mac.com       |
|  6 | Janine     | Labrune         | janine.labrune@aol.com         |
|  7 | Susan      | Nelson          | susan.nelson@qq.com            |
|  8 | Zbyszek    | Piestrzeniewicz | zbyszek.piestrzeniewicz@qq.com |
|  9 | Roland     | Keitel          | roland.keitel@yahoo.com        |
| 10 | Julie      | Murphy          | julie.murphy@yahoo.com         |
| 11 | Kwai       | Lee             | kwai.lee@google.com            |
| 12 | Jean       | King            | jean.king@qq.com               |
| 13 | Susan      | Nelson          | susan.nelson@qq.comt           |
| 14 | Roland     | Keitel          | roland.keitel@yahoo.com 

在一列中找到重复的值

1 SELECT 
2     email, 
3     COUNT(email)
4 FROM
5     contacts
6 GROUP BY email
7 HAVING COUNT(email) > 1;

输出如下

+-------------------------+--------------+
| email                   | COUNT(email) |
+-------------------------+--------------+
| janine.labrune@aol.com  |            2 |
| roland.keitel@yahoo.com |            2 |
+-------------------------+--------------+
2 rows in set

在多个列中查找重复值

 1 SELECT 
 2     first_name, COUNT(first_name),
 3     last_name,  COUNT(last_name),
 4     email,      COUNT(email)
 5 FROM
 6     contacts
 7 GROUP BY 
 8     first_name , 
 9     last_name , 
10     email
11 HAVING  COUNT(first_name) > 1
12     AND COUNT(last_name) > 1
13     AND COUNT(email) > 1;

输出

+------------+-------------------+-----------+------------------+-------------------------+--------------+
| first_name | COUNT(first_name) | last_name | COUNT(last_name) | email                   | COUNT(email) |
+------------+-------------------+-----------+------------------+-------------------------+--------------+
| Janine     |                 2 | Labrune   |                2 | janine.labrune@aol.com  |            2 |
| Roland     |                 2 | Keitel    |                2 | roland.keitel@yahoo.com |            2 |
+------------+-------------------+-----------+------------------+-------------------------+--------------+
2 rows in set

 

找重复值

原文:https://www.cnblogs.com/wangzhisdu/p/8297729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!