SQL 使用 COUNT 进行查询,计数 >1,显示重复项的完整详细信息
SQL Query with COUNT, Having Count >1, display full details of duplicates
我有一个 table 喜欢 :
name employment_Status email
---- ---- -----
David E David@email.com
John U John@email.com
Michael E Michael@email.com
Steve E Michael@email.com
James U David@email.com
Mary U Mary@email.com
Beth E Beth@email.com
我首先选择 email
和 count(email)
:
SELECT email, COUNT(email) AS emailCount
FROM Table
GROUP BY email
HAVING ( COUNT(email) > 1 );
当我也尝试包含 name
时出现问题:
SELECT name, email, COUNT(email) AS emailCount
FROM Table
GROUP BY name, email
HAVING ( COUNT(email) > 1 );
我想找到所有具有重复电子邮件地址的人(仅当两人都受雇时 (E))。但是它返回零结果。
我希望能够显示具有重复电子邮件且 employment_Status
E
的人的所有信息。如果两个人有相同的电子邮件,但其中一个或两个都是失业 (U),则忽略即可。
有人可以指点一下吗?
我想你想要 exists
:
select t.*
from t
where t.employeed = 'E' and
exists (select 1
from t t2
where t2.email = t.email and t2.employeed = 'E' and
t2.name <> t.name
);
请注意,这假设 name
(或至少 name
/email
)是唯一的。
在MySQL 8+中,您可以使用window函数:
select t.*
from (select t.*, count(*) over (partition by t.email) as cnt
from t
where t.employeed = 'E'
) t
where cnt >= 2;
一种方法是将您的查询用作 FROM 子句中的子查询,并将结果与主 table.
SELECT t.*, d.emailCount
FROM (
SELECT email, employment_Status, COUNT(*) AS emailCount
FROM my_table
GROUP BY email
WHERE employment_Status = 'E'
HAVING emailCount > 1
) d
JOIN my_table t USING(email, employment_Status)
您也可以使用 GROUP_CONCAT(name)
,如果您可以在(逗号)分隔的字符串中获取名称:
SELECT email, COUNT(*) AS emailCount, GROUP_CONCAT(name) as names
FROM my_table
GROUP BY email
WHERE employment_Status = 'E'
HAVING emailCount > 1
您的示例数据的结果将是:
email emailCount names
-----------------------------------------------
Michael@email.com 2 Michael,Steve
我有一个 table 喜欢 :
name employment_Status email
---- ---- -----
David E David@email.com
John U John@email.com
Michael E Michael@email.com
Steve E Michael@email.com
James U David@email.com
Mary U Mary@email.com
Beth E Beth@email.com
我首先选择 email
和 count(email)
:
SELECT email, COUNT(email) AS emailCount
FROM Table
GROUP BY email
HAVING ( COUNT(email) > 1 );
当我也尝试包含 name
时出现问题:
SELECT name, email, COUNT(email) AS emailCount
FROM Table
GROUP BY name, email
HAVING ( COUNT(email) > 1 );
我想找到所有具有重复电子邮件地址的人(仅当两人都受雇时 (E))。但是它返回零结果。
我希望能够显示具有重复电子邮件且 employment_Status
E
的人的所有信息。如果两个人有相同的电子邮件,但其中一个或两个都是失业 (U),则忽略即可。
有人可以指点一下吗?
我想你想要 exists
:
select t.*
from t
where t.employeed = 'E' and
exists (select 1
from t t2
where t2.email = t.email and t2.employeed = 'E' and
t2.name <> t.name
);
请注意,这假设 name
(或至少 name
/email
)是唯一的。
在MySQL 8+中,您可以使用window函数:
select t.*
from (select t.*, count(*) over (partition by t.email) as cnt
from t
where t.employeed = 'E'
) t
where cnt >= 2;
一种方法是将您的查询用作 FROM 子句中的子查询,并将结果与主 table.
SELECT t.*, d.emailCount
FROM (
SELECT email, employment_Status, COUNT(*) AS emailCount
FROM my_table
GROUP BY email
WHERE employment_Status = 'E'
HAVING emailCount > 1
) d
JOIN my_table t USING(email, employment_Status)
您也可以使用 GROUP_CONCAT(name)
,如果您可以在(逗号)分隔的字符串中获取名称:
SELECT email, COUNT(*) AS emailCount, GROUP_CONCAT(name) as names
FROM my_table
GROUP BY email
WHERE employment_Status = 'E'
HAVING emailCount > 1
您的示例数据的结果将是:
email emailCount names
-----------------------------------------------
Michael@email.com 2 Michael,Steve