编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
示例:
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
根据以上输入,你的查询应返回以下结果:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
说明:所有电子邮箱都是小写字母。
mysql查看重复数据, 可以按照email进行分组,然后使用having子句过滤count>1的数据,查询的结果就是重复的数据
# 创建表
CREATE TABLE `Person` (
`id` int(11) NOT NULL,
`email` varchar(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
# 插入测试数据
insert into Person (id,email) value (1,"a@b.com");
insert into Person (id,email) value (2,"c@d.com");
insert into Person (id,email) value (3,"a@b.com");
# 查找重复数据
select email from Person group by Email having count(email) > 1