select 10 rows per day with order

前端 未结 9 563
感情败类
感情败类 2021-02-06 16:08

i have a db with records with date (timestamp) i need to select 10 records for each day (there are many more per day) and order them by few columns...

how should that qu

9条回答
  •  借酒劲吻你
    2021-02-06 16:35

    You have to get your 10 records per day in a subquery for each day and join them to the main table by a left join, so you'll get max 10 records per day. The SQL would look like this:

    SELECT t1.columns
    FROM mytable t1 
      LEFT JOIN 
         (SELECT pk FROM mytable t2 
         WHERE t2.datecol = t1.datecol 
         ORDER BY t2.orderFor10Rows LIMIT 10) t3
      ON t1.pk = t3.pk
    ORDER BY t1.anyOtherColumns
    

    No warranty for proper MySQL-syntax as I'm not used to it.

提交回复
热议问题