If necessary, replace "id" with the name of your primary key:
(SELECT * FROM table1 WHERE criteria ORDER BY views DESC LIMIT 15) UNION (SELECT * FROM table1 WHERE criteria AND id NOT IN(SELECT id FROM table1 WHERE criteria LIMIT 15) ORDER BY date_upload DESC LIMIT 15)
This request:
- selects the top 15 entries matching the criteria sorted by views
- selects the top 15 matching criteria, not the first SELECT, and orders their date_upload
With this query you are sure to get 30 records each time 30 different records are available in the table.
source share