mcmuney Posted September 24, 2015 Share Posted September 24, 2015 I'm using the code below, which displays the most recent 15 results. I'd like to modify it so that it displays the most recent 15 results in random order. How can I achieve this within the code below without having to use another query? SELECT mem_id,type_id,date_added,value FROM sc_coins_asset WHERE value<>'0' ORDER BY date_added DESC LIMIT 0,15 Quote Link to comment https://forums.phpfreaks.com/topic/298296-how-to-display-random-results-within-selection/ Share on other sites More sharing options...
hansford Posted September 24, 2015 Share Posted September 24, 2015 You can try this, untested. There may be more robust ways of doing this. See if others chime in. "SELECT mem_id,type_id,date_added FROM(SELECT value FROM sc_coins_asset WHERE value<>'0' ORDER BY RAND() DESC LIMIT 0,15) u ORDER BY date_added" Quote Link to comment https://forums.phpfreaks.com/topic/298296-how-to-display-random-results-within-selection/#findComment-1521457 Share on other sites More sharing options...
Jacques1 Posted September 24, 2015 Share Posted September 24, 2015 Just use shuffle() in your application. There's no reason for doing this in MySQL. hansford's query is incorrect, because it yields 15 random rows which are then ordered by date (the selected fields also don't match). You want the reverse order of operations. But then again, why use MySQL in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/298296-how-to-display-random-results-within-selection/#findComment-1521458 Share on other sites More sharing options...
benanamen Posted September 24, 2015 Share Posted September 24, 2015 There's no reason for doing this in MySQL. I disagree. You should ALWAYS have the database do the work when it can. Quote Link to comment https://forums.phpfreaks.com/topic/298296-how-to-display-random-results-within-selection/#findComment-1521488 Share on other sites More sharing options...
QuickOldCar Posted September 24, 2015 Share Posted September 24, 2015 It's not good to do ORDER BY RAND() especially if have many rows, even if properly indexed is slow. It's only 15 latest results so wouldn't matter much either way. Quote Link to comment https://forums.phpfreaks.com/topic/298296-how-to-display-random-results-within-selection/#findComment-1521489 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.