找重复值

Posted 大明湖畔的守望者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了找重复值相关的知识,希望对你有一定的参考价值。

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

表中数据如下

| id | first_name | last_name       | email                          |
+----+------------+-----------------+--------------------------------+
|  1 | Carine     | Schmitt         | [email protected]          |
|  2 | Jean       | King            | [email protected]               |
|  3 | Peter      | Ferguson        | [email protected]      |
|  4 | Janine     | Labrune         | [email protected]         |
|  5 | Jonas      | Bergulfsen      | [email protected]       |
|  6 | Janine     | Labrune         | [email protected]         |
|  7 | Susan      | Nelson          | [email protected]            |
|  8 | Zbyszek    | Piestrzeniewicz | [email protected] |
|  9 | Roland     | Keitel          | [email protected]        |
| 10 | Julie      | Murphy          | [email protected]         |
| 11 | Kwai       | Lee             | [email protected]            |
| 12 | Jean       | King            | [email protected]               |
| 13 | Susan      | Nelson          | [email protected]           |
| 14 | Roland     | Keitel          | [email protected] 

在一列中找到重复的值

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

输出如下

+-------------------------+--------------+
| email                   | COUNT(email) |
+-------------------------+--------------+
| [email protected]  |            2 |
| [email protected] |            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 | [email protected]  |            2 |
| Roland     |                 2 | Keitel    |                2 | [email protected] |            2 |
+------------+-------------------+-----------+------------------+-------------------------+--------------+
2 rows in set

 

以上是关于找重复值的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法将2个非常相似的代码片段组合成一个函数并重复?

使用在另一个片段(NPE)中生成的值设置片段的 TextView [重复]

使用片段从另一个类获取值时出现 NullPointerException [重复]

如何将选定的对话框值设置为片段中的 TextViews [重复]

sublimetext3中保存代码片段

片段示例中的Android ListView [重复]