传送门

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

 

本章讲解 SELECT DISTINCT 语句。

SQL SELECT DISTINCT 语句

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

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

语法:

SELECT DISTINCT 列名称 FROM 表名称

使用 DISTINCT 关键词

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

SELECT Company FROM Orders

"Orders"表:

Company OrderNumber
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" 仅被列出了一次。

相关文章:

  • 2022-03-06
  • 2021-12-11
  • 2022-02-14
  • 2021-10-01
  • 2021-08-30
  • 2021-09-07
  • 2021-07-26
  • 2021-11-04
猜你喜欢
  • 2021-12-12
  • 2022-02-21
  • 2021-07-14
  • 2021-06-16
  • 2021-08-03
  • 2021-08-17
  • 2021-10-11
相关资源
相似解决方案