select 10 rows per day with order

前端 未结 9 542
感情败类
感情败类 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:39

    If you just need ten rows — any ten rows, you don't care which, and there is no guarantee they're random, you can use the LIMIT clause. Example:

    SELECT whatever
      FROM tablename
      WHERE datecol = '2009-07-13'
      LIMIT 10
    

    That'll give you ten rows. Its up to MySQL which ten. You can use ORDER BY or additional WHERE items to pick a certain 10. For example, here is the most recent 10:

    SELECT whatever
      FROM tablename
      WHERE datecol = '2009-07-13'
      ORDER BY timecol DESC
      LIMIT 10
    

    LIMIT is documented as part of the SELECT syntax.

提交回复
热议问题