0
点赞
收藏
分享

微信扫一扫

Mysql 分组后取时间最大的一条记录

秀妮_5519 2022-03-11 阅读 212

Mysql5.7

先按supervise_type 分组,取出时间最大的一条,然后在根据 supervise_type 和 max(start_date) 去原表查询相同的记录。

Mysql8.0就可以直接用窗函数解决了

SELECT
	a.id,
	a.supervise_type,
	a.start_date 
FROM
	t_inspect_round_copy1 a,
	(
	SELECT
		b.supervise_type AS "supervise_type",
		max( b.start_date ) AS "start_date" 
	FROM
		t_inspect_round_copy1 b 
	WHERE
		b.del_flag = '0' 
		AND b.`level` = '4' 
	GROUP BY
		b.supervise_type 
	) d 
WHERE
	a.del_flag = '0' 
	AND a.`level` = '4' 
	AND d.supervise_type = a.supervise_type 
	AND a.start_date = d.start_date 
ORDER BY
	a.supervise_type ASC
举报

相关推荐

0 条评论