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?
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?
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.