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