There is a table with some events that has a structure:

id,name,date , events have an annual periodicity (like holidays). It is necessary to select events in such a way that the next five events are displayed, if for example 10 events occur on a single day, then at each request, five random events were issued.

Resolved:

  select * from table where date>=CURRENT_DATE order by date,rand()limit 5 

    2 answers 2

    Plus order by rand() at the end and limit 5.

    • In this case, it will mix all the records that are older than the current day and as a result we will not get the next ones. - FLK
    • Ok, but if you try with join, for example, like this: select id, select date, date from table where date> 5) t2 on t1.id = t2. id order by rand (); - CrazyTimon
    • In this case, the query will select the 5 closest ones and then will sort them. As a result, the same elements will be displayed only in a random order. - FLK
    • Then the question is different. What is the time threshold for the number of events? those. There are 4 events on the first day, and 15 events on the second day, should the query output all 4 events of the 1st day and randomly one event of the second day? Or randomly 5 events from these two days? - CrazyTimon
    • 4 events of the first day and randomly 1 event of the second day. - FLK

    You need to get the record, the date of which exceeds the current - this will be coming