Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22 查找重复的电子邮箱 #27

Open
astak16 opened this issue Jan 13, 2022 · 0 comments
Open

22 查找重复的电子邮箱 #27

astak16 opened this issue Jan 13, 2022 · 0 comments
Labels

Comments

@astak16
Copy link
Owner

astak16 commented Jan 13, 2022

题目

查找 person 表中所有重复的电子邮箱

create table person (
	id int,
	email varchar(255)
);
insert into person values(1, '[email protected]'), (2, '[email protected]'), (3, '[email protected]');

SQL:方法一

select email from person group by email having count(email) > 1;

解析

通过 group byemail 分组,在使用 having 将重复的 email 筛选出来。

SQL:方法二

select t.email from (
	select email, count(email) num from person group by email
) t where t.num > 1;

解析

email 分组后计算出相同 email 的数量作为临时表,筛选出 num > 1 的邮箱

SQL:方法三

with temp as (
	select email, count(email) num from person group by email
) 
select email from temp where num > 1;

解析

使用 with 建立临时表,和方法二一样

SQL:方法四

select distinct person.email from person
left join person temp on person.email = temp.email
where person.id != temp.id;

解析

使用 left join 自连,连接条件是 person.eamil = temp.email 并且通过 whereperson.id != temp.id筛选出来,最后再通过 distinct 去重

@astak16 astak16 added the 简单 label Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant