CREATE TABLE t_testscore( pk_id INT PRIMARY KEY, c_name VARCHAR(50) , c_score INT, c_class INT )DEFAULT CHARSET=utf8;
INSERT INTO t_testscore VALUES (1, '張三6', 66, 1),(2, '張三5', 65, 1),(3, '張三4', 64, 1), (4, '張三3', 63, 1),(5, '張三2', 62, 1);
INSERT INTO t_testscore VALUES (11, '李四6', 76, 2),(12, '李四5', 75, 2),(13, '李四4', 74, 2), (14, '李四3', 73, 2),(15, '李四2', 72, 2);
SELECT * FROM t_testscore t
WHERE EXISTS(SELECT COUNT(*) FROM t_testscore ts WHERE ts.c_score>=t.c_score GROUP BY ts.c_class HAVING COUNT(*)<=3)
ORDER BY c_class,c_score DESC;
當使用group by 查詢時報錯: this is incompatible with sql_mode=only_full_group_by, 原因是: 在使用group by 時, select 后跟的查詢對象不是聚合函數,並且查詢對象(可能有多個)
沒有在group by 中出現,則會報 this is incompatible with sql_mode=only_full_group_by 錯誤.
例如在上表中使用查詢語句select c_name from t_testscore group by c_class就會報錯, 因為c_name 沒有在group by 中出現。
但是使用查詢語句 select count(*) from t_testscore group by c_class 就不會報錯, 因為count() 是聚合函數.