如表:
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

解决方案 »

  1.   

    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 limit 3; 这个只支持123,234、456这样的,不支持你那种环状的。要做到那种环状的,一条sql搞不定,得写存储过程,判断是否取出了几条结果,如果不足3条,就从开头开始取,补足3条。用php做也是这个思路。
      

  2.   

    用rand(1,8)获取随机整数,存放在一个长度为3的列表里,如果获取到的随机整数与之前的一样,重新获取,取到3个不同的数以后,以这3个数为ID到数据库里去取记录
      

  3.   


    上面有笔误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;