2017-05-31 107 views
-1

下面的查询:错误与GROUP BY语句

SELECT a.vendor_id, a.crew_type_id, count(a.vendor_id) as `totalVendor` 
FROM tb_vendor_crew_type_details as a 
WHERE a.market = 1 
    AND a.crew_available > 0 
    AND a.crew_type_id IN (161, 183, 220, 221, 227) 
Group by a.vendor_id 
Having totalVendor >= 5 

抛出一个错误:

SELECT list is not in GROUP BY clause and contains nonaggregated 
column 'scope_worker_dev.a.crew_type_id' which is not functionally 
dependent on columns in GROUP BY clause; this is incompatible with 
sql_mode=only_full_group_by 0.00037 sec 

可能是什么原因呢?我在查询中犯了什么错误?

回答

0

如果基于你的SELECT语句

SELECT a.vendor_id,a.crew_type_id,count(a.vendor_id) as `totalVendor` 
FROM tb_vendor_crew_type_details as a WHERE a.market = 1 AND a.crew_available 
> 0 
AND a.crew_type_id 
IN (161,183,220,221,227) Group by a.vendor_id, a.crew_type_id Having totalVendor >=5 
0

在MySQL中有充分组通过,如果是的sql_mode only_full_group_by,这意味着在select条款,应该只有在group by或其他柱聚集态功能列。

就像你的查询,你想做的事,应该是这样的:

SELECT a.vendor_id, count(a.vendor_id) as `totalVendor` 
FROM tb_vendor_crew_type_details as a 
WHERE a.market = 1 
AND a.crew_available > 0 
AND a.crew_type_id IN (161,183,220,221,227) 
Group by a.vendor_id Having totalVendor >= 5 

或者使用group_concat各组中Concat的crew_type_id

SELECT a.vendor_id, group_concat(a.crew_type_id) as crew_type_ids, count(a.vendor_id) as `totalVendor` 
FROM tb_vendor_crew_type_details as a 
WHERE a.market = 1 
AND a.crew_available > 0 
AND a.crew_type_id IN (161,183,220,221,227) 
Group by a.vendor_id Having totalVendor >= 5 

如果crew_type_id确实没有意义了如果您不修改您的查询,您可以从sql_mode删除only_full_group_by

set @@global.sql_mode = replace(lower(@@global.sql_mode), 'only_full_group_by', '');