Perform RANDOM SELECT [duplicate]

5

I have a table where I always want to display a single record in a random fashion.

I'm doing this:

SELECT 'id' FROM 'tabela' ORDER BY RAND() LIMIT 1

I see that in this way the same records repeat a lot, is there a better way to do it?

    
asked by anonymous 18.01.2016 / 10:21

1 answer

3

With MySQL 4.1 or later, you can do the following:

SELECT id FROM tabela WHERE id >= (SELECT FLOOR(MAX(id) * RAND()) FROM tabela) ORDER BY id LIMIT 1;

Note that this method only works on tables with unique IDs.

    
18.01.2016 / 10:52