Mixing of Group Columns with No Group Columns

2019-08-30 03:26发布

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'

3条回答
Rolldiameter
2楼-- · 2019-08-30 04:03

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'
查看更多
ら.Afraid
3楼-- · 2019-08-30 04:04

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'

查看更多
淡お忘
4楼-- · 2019-08-30 04:17

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'
查看更多
登录 后发表回答