• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

MySQL order by x, limit by y, and reorder by z

Wojtek

W as in Whisky
NLC
So I'd like to do the following:

Pull up the last 8 items ordered by added date, and reoder them randomly.

Unfortunately the following:
Code:
SELECT * FROM table ORDER BY added DESC LIMIT 8 ORDER BY RAND()
does not work :(

Any ideas?
 
Are you using PHP?
If you're using PHP, you can write a little function to extract the MySQL results as an array, then randomly organize the array and then return the results.
 
You can only have 1 "ORDER BY" statement in SQL

As Glenn said, I think your best bet is using PHP to randomise the output. The only other way to do such would be to use mysql's "JOIN" command and join 2 querys together, but thats VERY complex.
 
You can only have 1 "ORDER BY" statement in SQL

As Glenn said, I think your best bet is using PHP to randomise the output. The only other way to do such would be to use mysql's "JOIN" command and join 2 querys together, but thats VERY complex.

very complex and not worth doing.

With the ORDER BY statement, it tells MySQL output which column to order the results, not by which id or which value to randomize.

This may help you out;
http://jan.kneschke.de/projects/mysql/order-by-rand

Otherwise, PHP would honestly be your best option.
 
Back
Top