A word of warning, order by rand() can be very inefficient, particularly for pulling one value out of a big table. The database needs to generate a random number for every entry in the table, which can mount up on a table with a lot of entries. The database then needs to sort the table based on this non-indexed value, which is inefficient also.
Generally, I’d expect you’d be better to do it as a couple of queries…
If you want an individual entry, or only a few, I’d suggest running the following:
You can repeat the second two steps to get multiple random items, noting that you may want to reduce $max by 1 each time and add an "TableID NOT IN ()" condition for previous selections.
The line is a little more blurred if you’re pulling out a lot of entries, and you may want to keep everything in SQL as there’s obviously going to be some overhead every time a connection is made. There’s a good page here describing how to select random entries purely in SQL: