I need to query my database and partially order and randomize the results.
For instance,
SELECT * FROM products ORDER BY views DESC LIMIT 5
would result something like this:
id name views --------------------- 0 Product #1 2 1 Product #2 1 2 Product #3 0 3 Product #4 0 4 Product #5 0
Now, how can I add RAND() (or something similar) in the query so that the last three items (Products #3, #4 and #5) would appear in random order since they’ve got the same amount of views, but the first two still descending based on the same views-field?
I tried something like
SELECT * FROM products ORDER BY views, RAND() DESC LIMIT 5
but obviously it doesn’t work.
Thank you in advance.
maybe … you can retrieve the records first then sort randomly using other programming language