时间:2021-07-01 10:21:17 帮助过:240人阅读
然而经过实测,该办法在表空时返回也为空。考虑到表空的情形,加一个集合,如下:
SELECT random_num
FROM (
SELECT FLOOR(RAND() * 99999) AS random_num
FROM numbers
UNION
SELECT FLOOR(RAND() * 99999) AS random_num
) AS ss
WHERE "random_num" NOT IN (SELECT my_number FROM numbers)
LIMIT 1
通过union字段在表空时,直接获取一个随机数返回。
基本思路:随机排序。
select * from numbers order by rand() limit 1
这行命令就可以从表中随机选择一条记录。如何是随机选择多条,如下
select * from numbers order by rand() limit 10
据说,order by rand()的办法效率不好,本人也没有在记录多的表测过。在规模不大的前提下,用这个办法应该是最简单的。如果要考虑效率,请参考文末的链接。
How to Generate Random number without repeat in database using PHP?
mysql unique number generation
MySQL select 10 random rows from 600K rows fast
mysql生成不重复随机数(unique number generation)
标签:限制 nio row get target 商品 tail article 条件查询