选择随机行后,我希望能够选择紧随其后的n条记录。举例来说,假设n = 10
的表numbers_table
的第1列numbers
具有从1到10000的连续行,如果我随机选择65,我想将第65行返回到75.该查询而是返回10个随机行:
SELECT `number` FROM `numbers_table` ORDER BY RAND() LIMIT 10
以下内容也不起作用。它返回65,然后返回其后的任何其他连续序列(例如65
90
91
92
93
...)
SELECT `number` FROM `numbers_table` WHERE `number` >
(SELECT `number` FROM `numbers_table` ORDER BY RAND() LIMIT 1)
LIMIT 10
答案 0 :(得分:1)
我认为您想这样做
a[]
每次将计算 Rand()到where子句中的问题
答案 1 :(得分:1)
类似这样的东西:
select number
from numbers_table
where number > (
select number
from numbers_table
where number < (
select min(number) too_high_id from (
select number from numbers_table order by number desc limit 10
) highest_10
)
order by rand()
limit 1
)
order by number
limit 11
如果您不介意选择少于11行,则更为简单:
select number
from numbers_table
where number > (
select number
from numbers_table
order by rand()
limit 1
)
order by number
limit 11
如果您的数字中有空格,并且只希望数字在随机选择的数字的10以内的行,则最好这样做:
select numbers_table.number
from (
select number as pick
from numbers_table
order by rand()
limit 1
) pick
join numbers_table on number between pick and pick+10
这些在https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=7b954cd24ed140ec52155ed023f3eb61
中得到了展示