Home > Database > Mysql Tutorial > MySql从表结果集中随机取一条数据_MySQL

MySql从表结果集中随机取一条数据_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:30:43
Original
1209 people have browsed it

bitsCN.com

MySql从表结果集中随机取一条数据

 

[sql] Select * from (Select (@rowNo :=@rowNo+1) AS rowno, pwd from pur_admininfo, (Select @rowNo := 0) b) tmp where rowno=(Select Round(Rand() * 10000000 % (Select Count(*) from pur_admininfo))) limit 1;  
Copy after login

为了可以随机得到表结果集中的任何一条记录,这里乘以10000000,然后再取模,但不知道为什么更大的数字就不行了。

另一种实现方式:

[sql] SELECT * FROM pur_admininfo AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(rid) FROM pur_admininfo) - (SELECT MIN(rid) FROM pur_admininfo)) + (SELECT MIN(rid) FROM pur_admininfo)) AS rid) AS t2 WHERE t1.rid >= t2.rid ORDER BY t1.rid LIMIT 1;  
Copy after login

 

 

bitsCN.com
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template