首页  编辑  

mysql 8.0 查询每个分组top n条记录的最简单方便的方法

Tags: /MySQL/   Date Created:
mysql 8.0支持rank函数了,可以用rank函数实现
SELECT * FROM (
	SELECT dev_id, di_channel, di_preset, di_capture_time, di_filename, RANK() OVER (
		PARTITION BY dev_id, di_channel, di_preset
		ORDER BY di_capture_time DESC
	) seq
	FROM tb_device_image 
	WHERE di_capture_time > DATE_SUB(NOW(), INTERVAL 3 DAY)
) a
WHERE seq < 4