Mysql query with RAND

Asked

Viewed 92 times

1

Is there any way I can bring Mysql records, where the field (for example) is_new is = 1, but when there are no records with the fields is_new = 1, I want the query to make an ORDER BY Rand(), it is possible?

In practice, every time I make a select, I want to bring in 5 clients, using the order by Rand, but when I have new records (and for that the is_new field of the client table gets 1) I want it to bring in 5 of these last records, so when records are displayed their flag changes to 0 and in the next query, it only comes from Rand

Thank you

1 answer

2

With that query he takes 5 records, always prioritizing the is_new:

SELECT campos FROM tabela ORDER BY is_new != 1, RAND() LIMIT 5
  • When the is_new is 1, the words is_new != 1 will return false, leaving the record first.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.