MySQL: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sss
阿新 • • 發佈:2019-01-05
今天在執行MySQL中sql語句的時候報錯了,執行的sql語句:
SELECT
prov_desc,
area_desc,
month_id,
MAX(total_fee)AS max_total,
FROM
sss
WHERE
prov_id = '075'
OR prov_id IN('017')
AND month_id IN('201207')
GROUP BY
prov_id,
prov_desc,
prov_ord,
area_desc,
area_ord
HAVING
MAX(total_fee)> 100
ORDER BY
prov_ord DESC,
area_ord
錯誤提示如下:
Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sss.month_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
認真看錯誤的提示發現是在group by中的欄位比較selelct中的欄位差了一個month_id, 而正好是month_id報錯。this is incompatible with sql_mode=only_full_group_by
後來發現Order by排序條件的欄位也必須要在group by內,看此大神的博文解釋之後豁然開朗,排序的欄位也是從分組的欄位中取出。 不明白的可以去看一下。
解決辦法:select欄位必須都在group by分組條件內(含有函式的欄位除外)。(如果遇到order by也出現這個問題,同理,order by欄位也都要在group by內)。