开发者

pick random number from a database which doesn't gets picked next time

开发者 https://www.devze.com 2023-01-08 07:52 出处:网络
I want to pick a random number from a database say 1, when I query again say 10 times I\'m querying i开发者_Python百科t should return all different random numbers.Use ORDER BY RAND()

I want to pick a random number from a database say 1, when I query again say 10 times I'm querying i开发者_Python百科t should return all different random numbers.


Use ORDER BY RAND()

If you want to get just one random record, your query should look like:

SELECT * FROM table ORDER BY RAND() LIMIT 1


If you only want a random number, don't use a database, use a random number generator. If you don't want repeats, simply keep track of the random numbers you've seen before and, if you pick one again, increment the new number by one until you reach a number you haven't seen yet.

If you want, say 10, random records from the database, then use @sAc's solution, but get them all at the same time. That will ensure that you don't have repeats in your selection. If you must select them one at a time, use the same technique as for the random number and keep track of the records you've seen before. Don't use the LIMIT directive, but simply select the first record you haven't seen on each iteration.

0

精彩评论

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

关注公众号