November 6th, 2012, 10:56 AM
Selecting a random row from a result list
Is it possible to select a certain (randomly generated) row from a query. So if a query normally has 91 results, than I just want one single result (randomly picked).
November 6th, 2012, 11:05 AM
SELECT * FROM
(my query here [LIMIT 91 if applicable]) x
ORDER BY RAND() LIMIT 1;
November 6th, 2012, 03:20 PM
when you say "normally" does this mean you are actually running the query and returning 91 rows somewhere, or do you want to ~not~ return them all, just one of them at most, ever?
Originally Posted by Baconcutter
November 6th, 2012, 03:40 PM
Well r937 I would say BULLSEYE. I use a query in a combination with PHP. From PHP I sent a SQL-query to MySQL, which than has a number of rows he can return (count(*)). Than I'll use Rand(1,<number of returning rows>) to create a random number. And than fetch the row from the database equaling the number we just created. But if we could skip all of this and build it all in in one query than it would just be great. Thats why the review from cafelatte looks appealing, but I do not know if it returns just one row.