I have a query:
$msg_check = mysql_query ("SELECT * FROM msgs WHERE name='$name' ORDER BY replies DESC LIMIT 5");
while($row = mysql_fetch_array($msg_check)) {
$comment = $row['comment'];
$nickname = $row['nickname'];
What I need to do is take 5 comments with most replies and sort them randomly. I've tried by combining RAND with DESC, but failed :/
You can apply an outer query that uses RAND() in the ORDER BY clause:
SELECT *
FROM (
SELECT *
FROM msgs
WHERE name='$name'
ORDER BY replies DESC LIMIT 5) AS t
ORDER BY RAND()
Note: It is always preferable to explicitly name each field in the SELECT clause instead of using *.
Try this
SELECT * FROM
(SELECT * FROM msgs where name='$name' ORDER BY replies DESC LIMIT 5)
ORDER BY rand()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With