Perform random SELECT

Asked

Viewed 186 times

5

I have a table where I always want to show a single record randomly.

I’m doing like this:

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

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

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.