Mysql查询 - 每个日期最多3个数据

时间:2012-11-08 06:59:48

标签: php mysql

enter image description here

这些是我的数据库表数据,{我只保留了一个表}

我需要从每个start_date中获取最多3个数据,

给我任何开发查询的想法,

2 个答案:

答案 0 :(得分:1)

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 

答案 1 :(得分:1)

我可能会建议你这个查询 -

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;

它会在event_id组中选择最少start_date的3行。