假设数据库表具有值
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
注意:此模式不是必需的(排序顺序和1-30之间的值)。
所以使用查询我可以得到10-20之间的值,依此类推。
但在这里,我想知道如何从不同范围获取值。 即:
......等等。
因此查询的输出将如下所示:
[2,7,14,16,23,29,...]
或
[1,6,11,18,22,26,...]
我有什么办法可以做到这一点。
答案 0 :(得分:2)
尝试这个
(SELECT id as r1 FROM `t1` WHERE id between 1 and 5 ORDER BY RAND() limit 0,1)
union
(SELECT id as r1 FROM `t1` WHERE id between 6 and 10 order by RAND() limit 0,1)
union
(SELECT id as r1 FROM `t1` WHERE id between 11 and 15 order by RAND() limit 0,1)
union
(SELECT id as r1 FROM `t1` WHERE id between 16 and 20 order by RAND() limit 0,1)
union
(SELECT id as r1 FROM `t1` WHERE id between 21 and 25 order by RAND() limit 0,1)
union
(SELECT id as r1 FROM `t1` WHERE id between 26 and 30 order by RAND() limit 0,1)
这是指定范围的查询,如果你想让它为你需要的动态范围,一旦你的所有范围都达到N,就动态地进行这个查询。