mysql分组后获取每个组排序后的第一条数据(整行)

有一个学生分数表student,数据结构是这样的

CREATE TABLE `student` (
`id` int(11) NOT NULL,
`student_id` int(11) DEFAULT NULL,
`line` int(11) DEFAULT NULL,
`subject_type` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 

我想根据学生分组  ,得到每个分组中分数最高的那一条数据:

大约20万数据

正确写法:

 1 SELECT
 2     max.*, MAX(max.line) lineMax
 3 FROM
 4     (
 5         SELECT DISTINCT
 6             (a.id) tt,
 7             a.*
 8         FROM
 9             student a
10         ORDER BY
11             a.line DESC
12     ) max
13 GROUP BY
14     student_id
distinct

  DISTINCT(主键)让数据集先进行排序再分组取排序后的第一条

但是:sharding-jdbc不支持此操作

分库分表后,就成为了一个阉割型的数据库。很多sql的特性是不支持的,需要使用其他手段改进。以下以3.0.0版本进行描述。

distinct

having

sharding-jdbc不支持having,可使用嵌套子查询进行替代

union

sharding-jdbc不支持union(all),可拆分成多个查询,在程序拼接

关于子查询

sharding-jdbc不支持在子查询中出现同样的表,

由于归并的限制,子查询中包含聚合函数目前无法支持。


 

转自:https://blog.csdn.net/persistencegoing/article/details/92764058

https://shardingsphere.apache.org/document/current/cn/features/sharding/use-norms/sql/

原文地址:https://www.cnblogs.com/zt007/p/14962512.html