leetcode with MySQL:182. Duplicate Emails

题目:

Table: Person

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| email | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.

Write an SQL query to report all the duplicate emails.

Return the result table in any order.

给定一个table,找到重複出现的Email

SELECT DISTINCT a.EmailFROM Person AS a, Person AS bWHERE a.Email = b.Email and a.id <> b.id;

将该表(a表)跟另一个自己(b表)比较
条件设两表Email相同但id不同者
然后在其中不重複的选出Email(DISTINCT)
最后执行时间311ms(faster than 88.85%)

另外我在讨论区有看到用GROUP BY的解法
起初这语法我并不知道
但在了解过后大概知道它的功用了

SELECT EmailFROM PersonGROUP BY EmailHAVING COUNT(Email) > 1;

透过GROUP BY将资料依Email分类
再选出其中Email出现次数(COUNT)超过1的
最后执行时间278ms(faster than 99.31%)

那我们下题见


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章