前言:
在開發(fā)自己博客的時候蒂破,在 Centos 中 MySQL 客戶端中使用 GROUP BY 相關(guān)查詢語句关噪,發(fā)現(xiàn)了一個兼容錯誤。但在 win 版本的 MySQL 并未發(fā)現(xiàn)該錯誤布隔。甚是不解趟济。。送爸。
相關(guān) SQL 語句和錯誤的描述
SELECT
t2.id,
t2.typeName,
COUNT( t1.id ) AS blogCount
FROM
t_blog t1
RIGHT JOIN t_blogtype t2 ON t1.typeId = t2.id
GROUP BY
t2.typeName
ORDER BY
t2.orderNo;
錯誤:
Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'db_blog.t2.id' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
上面的解釋可為:db_blog.t2.id 在 GROUP BY 中違背了 mysql 的規(guī)則铛嘱。
進(jìn)過自己的一番百度與 Google暖释,有人說要改變 sql_model 的方式,但我感覺這樣不好墨吓,終于解決了問題球匕,
發(fā)現(xiàn) MySQL: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sss 這篇博客有說道:
select 字段必須都在 group by 分組條件內(nèi)(含有函數(shù)的字段除外)。(如果遇到 order by 也出現(xiàn)這個問題帖烘,同理亮曹,order by 字段也都要在group by內(nèi) )
于是乎, GROUP BY 中加入 t2.id秘症,成功解決問題照卦。
SELECT
t2.id,
t2.typeName,
COUNT( t1.id ) AS blogCount
FROM
t_blog t1
RIGHT JOIN t_blogtype t2 ON t1.typeId = t2.id
GROUP BY
t2.typeName,
t2.id
ORDER BY
t2.orderNo;