leetcode - 數據庫 - 182. 查找重複的電子郵箱

      
      原題:https://leetcode-cn.com/problems/duplicate-emails/
      我自己做leetcode的數據庫題目的解題記錄:
              解題目錄 https://blog.csdn.net/weixin_42845682/article/details/105196004
      
      

題目描述:

      編寫一個 SQL 查詢,查找 Person 表中所有重複的電子郵箱。

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

      根據以上輸入,你的查詢應返回以下結果:

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

      

答案:

第一種答案

      感覺這道題很簡單,沒啥做的。。。

select
    distinct(p1.Email) email
from Person p1
join Person p2 on p1.email = p2.email and p1.id != p2.id

第二種答案

      我感覺,這道題應該有好幾種解決辦法,而且第一種辦法看的好蠢。。。

select 
    Email 
    from 
    (
		select 
		    Email,
		    count(Email) c 
		from Person 
		group by Email 
	) tmp 
where c >1

第三種答案

      第二種答案看着好麻煩…其實用having省略一下就行。

select 
    Email
from Person 
group by Email
having count(Email) >1

      
      
      
      
      
      
      
      

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章