原因是5.7后默認(rèn)開啟了
.mysql 5.7中的sql_mode的值為:
2.ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
解決方式實(shí)戰(zhàn)
1.方式一
我直接在window環(huán)境下修改my.ini文件,
添加或者修改 sql_mode 娶耍,然后重新啟動(dòng)服務(wù)热幔。
[mysql]
sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
2.方式二
在 mysql 中有一個(gè)函數(shù): any_value(field) 允許鸳址,非分組字段的出現(xiàn)
所以 使用sql查詢語句中不需要group by的字段上可以使用any_value()函數(shù)
SELECT ANY_VALUE(id)as id,ANY_VALUE(uid) as uid ,ANY_VALUE(username) as username,ANY_VALUE(title) as title,ANY_VALUE(author) as author,ANY_VALUE(thumb) as thumb,ANY_VALUE(description) as description,ANY_VALUE(content) as content,ANY_VALUE(linkurl) as linkurl,ANY_VALUE(url) as url,ANY_VALUE(group_id) as group_id,ANY_VALUE(inputtime) as inputtime, count(id) as count FROM `news` GROUP BY `group_id` ORDER BY ANY_VALUE(inputtime) DESC LIMIT 20