Is it possible to sort the "first n" records randomly with just one SELECT?

Is there a way to do this without two options?

Original request

SELECT name,view_count FROM `ex`.`item` where status='available' order by view_count asc limit 40; 

For random display

 SELECT * FROM (SELECT name,view_count FROM `ex`.`item` where status='available' order by view_count asc limit 40 ) AS temp ORDER BY RAND(); 

Can this be done without a second choice?

+4
source share
1 answer

Try the following:

SQL Fiddle: http://sqlfiddle.com/#!2/330f8/2

 SELECT name, view_count FROM `ex`.`item` where status='available' order by rand(), view_count asc limit 40; 
+1
source

Source: https://habr.com/ru/post/1501962/


All Articles