leetcode 182. Duplicate Emails

传送门

182. Duplicate Emails

My Submissions
Total Accepted: 14498 Total Submissions: 38364 Difficulty: Easy

Write a SQL query to find all duplicate emails in a table named Person.

+----+---------+
| Id | Email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+

For example, your query should return the following for the above table:

+---------+
| Email   |
+---------+
| a@b.com |
+---------+

Note: All emails are in lowercase.

Subscribe to see which companies asked this question

1 # Write your MySQL query statement below
2 select distinct p1.Email from 
3     Person p1,Person p2 
4         where p1.Email=p2.Email 
5             and p1.Id!=p2.Id

about distinct

SQL SELECT DISTINCT 语句

本章讲解 SELECT DISTINCT 语句。

SQL SELECT DISTINCT 语句

在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。

关键词 DISTINCT 用于返回唯一不同的值。

语法:

SELECT DISTINCT 列名称 FROM 表名称

使用 DISTINCT 关键词

如果要从 "Company" 列中选取所有的值,我们需要使用 SELECT 语句:

SELECT Company FROM Orders

"Orders"表:

CompanyOrderNumber
IBM 3532
3wschool 2356
Apple 4698
3wschool 6953

结果:

Company
IBM
3wschool
Apple
3wschool

请注意,在结果集中,3wschool 被列出了两次。

如需从 Company" 列中仅选取唯一不同的值,我们需要使用 SELECT DISTINCT 语句:

SELECT DISTINCT Company FROM Orders 

结果:

Company
IBM
3wschool
Apple

现在,在结果集中,"3wschool" 仅被列出了一次。

原文地址:https://www.cnblogs.com/njczy2010/p/5193373.html