我需要使用MySQL从表中获取一组可重复的随机行。我使用bigint函数实现了这一点,并使用行的MySQL主键作为种子。有趣的是,这产生的数字看起来一点也不随机。有人能告诉我这是怎么回事吗?如何让它正常工作?
select id from foo where rand(id) < 0.05 order by id desc limit 100在一个示例中,600行中没有一行被返回。我将select更改为包含"id,rand(id)“,并在where this is the I get中去掉了rand子句:
| 163345 | 0.315191733944408 |
| 163343 | 0.814825518815616 |
| 163337 | 0.313726862253367 |
| 163334 | 0.563177533972242 |
| 163333 | 0.312994424545201 |
| 163329 | 0.312261986837035 |
| 163327 | 0.811895771708242 |
| 163322 | 0.560980224573035 |
| 163321 | 0.310797115145994 |
| 163319 | 0.810430896291911 |
| 163318 | 0.560247786864869 |
| 163317 | 0.310064677437828 |看看有多少0.31xxx行。完全不是随机的。
PS:我知道这很慢,但在我的应用程序中,where子句将行数限制在1000行以内。
发布于 2011-11-01 20:34:00
为此,对所有行使用相同的种子,如下所示:
select id from foo where rand(42) < 0.05 order by id desc limit 100请参阅rand()文档,了解它为什么会这样工作。如果需要另一组值,请更改种子。
发布于 2011-11-01 20:49:42
将id返回的十进制数相乘:
select id from foo where rand() * id < 5 order by id desc limit 100https://stackoverflow.com/questions/7966430
复制相似问题