MySQL Query with RAND

1

Is there any way I can get MySQL records, where the is_new field is = 1, but when there are no records with the is_new = 1 field, I want the query to do an ORDER BY rand () is it possible?

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

Thank you

    
asked by anonymous 22.06.2016 / 04:49

1 answer

2

With this query it fetches 5 records, always prioritizing is_new :

SELECT campos FROM tabela ORDER BY is_new != 1, RAND() LIMIT 5
  • When is_new is 1, the expression is_new != 1 will return false , leaving the record first.
22.06.2016 / 04:53