Home  >  Article  >  Backend Development  >  随机抽取三条连续的记录

随机抽取三条连续的记录

WBOY
WBOYOriginal
2016-06-13 11:11:03924browse

随机抽取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; 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn