mysql報錯及其解決方式
1、在使用group by 查詢一張表的數據的時候:select date,time,max(delaytime) as delaytime,sum(delaynum) as delaynum, max(onlineCount) as onlineCount,sum(perMinuteVerify) as perMinuteVerify,auditor
from verifyDelayLog WHERE `date` = '2016-06-29' group by time;
報錯如下:
ERROR 1055 (42000): Expression #7 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'postscan.verifyDelayLog.auditor' which is not functionally dependent on columns
in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
原因:使用 select @@sql_mode; 命令可以看到,數據庫設置了 ONLY_FULL_GROUP_BY 的mode,意思就是:
對於GROUP BY聚合操作,如果在SELECT中的列,沒有在GROUP BY中出現,那么這個SQL是不合法的,因為列不在GROUP BY從句中
所以對於設置了這個mode的數據庫,在使用group by 的時候,就要用MAX(),SUM(),ANT_VALUE()這種聚合函數,才能完成GROUP BY 的聚合操作。