Skip navigation links

MySQL Forums :: Quality Assurance :: Retrieve a random row, but fast.


Advanced Search

Re: Retrieve a random row, but fast.
Posted by: Tom T ()
Date: July 22, 2008 07:20AM

Or you could do it with a subselect, which is even simpler (in my humble opinion, that is...):

SELECT *
FROM myTable,
(SELECT FLOOR(MAX(myTable.id) * RAND()) AS randId FROM myTable) AS someRandId
WHERE myTable.id = someRandId.randId

The inner SELECT gives you a random id in the right range.
The outer SELECT looks for the right row in the table.

Options: ReplyQuote


Subject Views Written By Posted
Retrieve a random row, but fast. 11111 Nils A. 11/28/2007 07:21AM
Re: Retrieve a random row, but fast. 5453 Nils A. 11/28/2007 07:24AM
Re: Retrieve a random row, but fast. 4699 Nils A. 12/06/2007 09:02PM
Re: Retrieve a random row, but fast. 5156 John Verplancke 12/11/2007 12:42AM
Re: Retrieve a random row, but fast. 40610 Jay Paroline 02/04/2008 02:52AM
Re: Retrieve a random row, but fast. 8464 Tom T 07/22/2008 07:20AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.