简体   繁体   中英

Mysql Select * Where Date is Today And Order Random limit 10

I Have Been thinking of the way to do this for almost a week now.

  • i need to select data from mysql database where date created is today and limit by 10
  • if there is no data created today selection will be random and limit by 10
  • if data created today is less than 10, it should select them and random the rest,
    For example: if 3 rows of data are created today, they should be selected first and random the 7 remaining without considering date.

Make three different queries and combine their data with UNION and apply limit on UNION and on individual queries as well. Please keep the order same in which you need the data. This should resolve your problem easily.

Here is example:

SELECT * FROM (
    SELECT ... FROM ... WHERE ... LIMIT 10
    UNION
    SELECT ... FROM ... WHERE ... LIMIT 10
    UNION
    SELECT ... FROM ... WHERE ... LIMIT 10
) as something LIMIT 10;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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