找重复值
在本教程中,您将学习如何在MySQL中找到一个或多个列的重复值。

在开始之前

由于原因很多,数据库中的重复事件发生很多。查找重复值是使用数据库时必须处理的重要任务之一。
对于演示,我们将创建一个名为contacts表,其中包含四个列:idfirst_namelast_nameemail
USE testdb;
CREATE TABLE contacts (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(255) NOT NULL
);
以下语句将行插入到contacts表中:
INSERT INTO contacts (first_name,last_name,email)
VALUES ('Carine ','Schmitt','[email protected]'),
('Jean','King','[email protected]'),
('Peter','Ferguson','[email protected]'),
('Janine ','Labrune','[email protected]'),
('Jonas ','Bergulfsen','[email protected]'),
('Janine ','Labrune','[email protected]'),
('Susan','Nelson','[email protected]'),
('Zbyszek ','Piestrzeniewicz','[email protected]'),
('Roland','Keitel','[email protected]'),
('Julie','Murphy','[email protected]'),
('Kwai','Lee','[email protected]'),
('Jean','King','[email protected]'),
('Susan','Nelson','[email protected]'),
('Roland','Keitel','[email protected]');
然后,查询表中的数据如下 -
SELECT
*
FROM
contacts;
执行上面查询,得到以下结果 -
+----+------------+-----------------+--------------------------------+
| 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 |
+----+------------+-----------------+--------------------------------+
14 rows in set
contacts表中,有一些行在first_namelast_nameemail列中具有重复的值,下面来看看如何查询它们。
在一列中找到重复的值
在基于一列的表中找到重复值,则使用以下语句:
SELECT
col,
COUNT(col)
FROM
table_name
GROUP BY col
HAVING COUNT(col) > 1;
如果表中出现多个值,则该值将被视为重复。在这个语句中,使用COUNT函数的GROUP BY子句来计算指定列(col)的值。HAVING子句中的条件仅包含值count大于1的行,这些行是重复的行。
可以使用此查询在contacts表中查找具有重复email的所有行,如下所示:
SELECT
email,
COUNT(email)
FROM
contacts
GROUP BY email
HAVING COUNT(email) > 1;
以下显示查询的输出:
+-------------------------+--------------+
| email | COUNT(email) |
+-------------------------+--------------+
| janine.labrune@aol.com | 2 |
| roland.keitel@yahoo.com | 2 |
+-------------------------+--------------+
2 rows in set
如上查询结果中可以看到,有一些行具有相同的电子邮件。
在多个列中查找重复值
有时,希望基于多个列而不是一个查找重复。在这种情况下,您可以使用以下查询:
SELECT
col1, COUNT(col1),
col2, COUNT(col2),
...
FROM
table_name
GROUP BY
col1,
col2, ...
HAVING
(COUNT(col1) > 1) AND
(COUNT(col2) > 1) AND
...
只有当列的组合重复时,行才被认为是重复的,所以在HAVING子句中使用了AND运算符。
例如,要使用first_namelast_nameemail列中的重复值在contacts表中查找行,请使用以下查询:
SELECT
first_name, COUNT(first_name),
last_name, COUNT(last_name),
email, COUNT(email)
FROM
contacts
GROUP BY
first_name ,
last_name ,
email
HAVING COUNT(first_name) > 1
AND COUNT(last_name) > 1
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
在本教程中,您已经学会了如何根据MySQL中一个或多个列的值来找到重复的行。
Copy link