这些是我的数据库表数据,{我只维护了一个表}
我需要从每个 start_date 获取最多 3 个数据,
给我任何开发查询的想法,
SELECT a.*
FROM Table1 a
INNER JOIN Table1 b ON a.start_date = b.start_date AND a.event_id <= b.event_id
GROUP BY a.event_id,a.start_date
HAVING COUNT(*) <= 3
ORDER BY a.start_date
我可能会建议你这个查询 -
SELECT * FROM (
SELECT t1.*, COUNT(*) pos FROM table t1
LEFT JOIN table t2
ON t2.start_date = t1.start_date AND t2.event_id <= t1.event_id
GROUP BY
t1.start_date AND t1.event_id
) t
WHERE
pos <= 3;
它在一个组中选择 3 行,其中 minevent_id
为一start_date
组。