Today I came across a situtation where I needed to have a particular set of MySQL database results to be in a random order. This was for an image slideshow that was being employed on a new project we are working on, the client wanted a random order to the pictures.

I was considering just randomising the results with some php after the data had been loaded from the database. However I did some research and came across the MySQL RAND() function, which can be used to randomise the order of the results. See the code here:

SELECT * FROM gallery_pics ORDER BY RAND()`

This returns a set of results that have been randomised by the row id. This function can be used with the LIMIT function to select how many rows you want to get and randomise. It is worth noting that using RAND() in conjuction with ORDER BY is only really suitable for small tables (sub 500 rows). Anything larger than this then the processing time gets too high to be practical.