开发者

Select random line in SQL database

I would like to select a random line in my database. I saw th开发者_开发技巧is solution on a website:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

This SQL query run but someone said me that it was a non performant query. Is there another solution ?

Thx


It is. You have to count rows number with

SELECT COUNT(*) FROM `table`;

After this with php function mt_rand() get the random from 1 to $count and get it with query:

SELECT `column` FROM `table` LIMIT $rand, 1


There's a lot more discussion of this subject, including performance implications and strategies for different DBMSs, in this question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜