简体   繁体   English

SQL SELECT DISTINCT和GROUP BY查询无效

[英]SQL SELECT DISTINCT and GROUP BY query not working

I have a tabel with e-mail adresses and additional data. 我有一个包含电子邮件地址和其他数据的表格。 Now i want to get all e-mailadresses that exsist multiple times in the table with different genders. 现在我想让所有在不同性别的桌子中出现多次的电子邮件。 I tried the folowing query: 我尝试了下面的查询:

SELECT email, COUNT(DISTINCT(gender)) FROM `email_data` WHERE COUNT(DISTINCT(gender)) > 1 GROUP BY email

I get the folowing result: #1111 - Invalid use of group function 我得到了以下结果:#1111 - 无效使用组功能

I tried different variations including GROUP BY email, gender. 我尝试了不同的变体,包括GROUP BY电子邮件,性别。 but nothing seems to work. 但似乎没什么用。 If i remove the WHERE part of the query it works. 如果我删除查询的WHERE部分它的工作原理。 Can someone help me out? 有人可以帮我吗?

You can't put aggregation functions in the WHERE clause. 您不能将聚合函数放在WHERE子句中。 They go in the HAVING clause. 他们进入HAVING条款。 However, it is usually more efficient to just compare the max and min values for this purpose: 但是,为此目的比较最大值和最小值通常更有效:

SELECT email, COUNT(DISTINCT gender)
FROM `email_data`
GROUP BY email
HAVING MIN(gender) <> MAX(gender)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM