简体   繁体   English

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

[英]Mysql Query- for each date max 3 datas

在此处输入图片说明

these is my DB table data,{ i have maintained only one Table}这是我的数据库表数据,{我只维护了一个表}

I need to fetch max 3 data from each start_date,我需要从每个 start_date 中获取最多 3 个数据,

give me any idea to develop query,,给我任何想法来开发查询,

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 

I may suggest you this query -我可能会建议你这个查询 -

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;

It selects 3 rows with min event_id in a start_date group.它在start_date组中选择具有最小event_id 3 行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM