Mixing of Group Columns with No Group Columns

你离开我真会死。 提交于 2019-12-13 03:57:35

问题


I got this error and I'm not sure how to work with it. Simple explanations would be much appreciated. Error:

1140: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause - SELECT email, COUNT(*) AS records_found FROM (emails) WHERE email = 'Email Address'


回答1:


You can't use aggregate functions (like count(*)) with non-aggregated columns (like email) unless you include a group by clause. I'm assuming you are intending to try and get the count of each distinct email in the emails table, which would require a group by clause added to your query, like this:

select email, count(*) as records_found
from (emails) 
where email = 'Email Address'
group by email

Given that you are using a where clause that will ensure a distinct single email, you may be wondering why it's required - you can alternatively simply add an aggregate function around the email column as well since you know it is distinct:

select max(email), count(*) as records_found
from (emails) 
where email = 'Email Address'



回答2:


If you want to use aggregate functions (MIN, MAX, COUNT), you need to provide MySQL with information on which columns to perform the aggregation on. For example in your case you should use

SELECT email, COUNT(*) AS records_found FROM (emails) GROUP BY email 

to get number of rows with each of the email addresses, or if you're simply looking for a number of rows with a given address do this:

SELECT COUNT(*) AS records_found FROM (emails) WHERE email = 'Email Address'



回答3:


You have email in the output, but you are not grouping by it. You most likely want to remove the email column from the output since you have it in the WHERE clause:

SELECT COUNT(*) AS records_found FROM (emails) WHERE email = 'Email Address'



来源:https://stackoverflow.com/questions/1712408/mixing-of-group-columns-with-no-group-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!