MySQL Forums
Forum List  »  Quality Assurance

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
11834
November 28, 2007 07:21AM
5700
November 28, 2007 07:24AM
4925
December 06, 2007 09:02PM
5374
December 11, 2007 12:42AM
41615
February 04, 2008 02:52AM
Re: Retrieve a random row, but fast.
8768
July 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.