以下查询:
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 :(得分:0)
假设根据您的选择陈述
拥有完整的分组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
答案 1 :(得分: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
在每个小组中连结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
对您没有意义且没有修改您的查询,则可以从only_full_group_by
删除sql_mode
:
set @@global.sql_mode = replace(lower(@@global.sql_mode), 'only_full_group_by', '');