随机抽取三条连续的记录
时间:2021-07-01 10:21:17
帮助过:14人阅读
随机抽取3条连续的记录?
如表:
temID
1
2
3
4
5
6
7
8
如果取3条记录,结果可以是:
1
2
3
或
4
5
6
或
7
8
1
或
8
1
2
等等
想不出mysql如何写?请问 用php 取出来后有无好的办法 ,进行随机抽取
随机抽取?php?mysql
------解决方案-------------------- 用rand(1,8)获取随机整数,存放在一个长度为3的列表里,如果获取到的随机整数与之前的一样,重新获取,取到3个不同的数以后,以这3个数为ID到数据库里去取记录
------解决方案-------------------- 引用: 781和812还算连续数据嘛?你这成环形链表了。
select * from test as t1 join (select round(rand() * (select max(temID) from `test`)) as id) as t2
where t1. temID >= t2. temID
order by t1. temID asc limi……
上面有笔误
select * from test as t1 join (select round(rand() * (select max(temID) from `test`)) as temID) as t2
where t1.temID >= t2.temID
order by t1.temID asc limit 3;