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() 是聚合函数.