2017-05-31 126 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', '');