LeetCode-182.查找重复的电子邮箱

题目:

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

表: Person
+----+---------+
| Id | Email   |
+----+---------+
| 1  | [email protected] |
| 2  | [email protected] |
| 3  | [email protected] |
+----+---------+

返回如下结果

+---------+
| Email   |
+---------+
| [email protected] |
+---------+
添加表结构和数据
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person`  (
  `Id` int(11) NOT NULL,
  `Email` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`Id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

INSERT INTO `person` VALUES (1, '[email protected]');
INSERT INTO `person` VALUES (2, '[email protected]');
INSERT INTO `person` VALUES (3, '[email protected]');
思路:
# 查重复的,count()>1
解答:
SELECT Email  FROM Person 
GROUP BY Email 
HAVING count( Email ) >1

你可能感兴趣的:(#,数据库,mysql,leetcode)