Home >Database >Mysql Tutorial >MySql查询随机几条数据_MySQL

MySql查询随机几条数据_MySQL

WBOY
WBOYOriginal
2016-06-01 13:37:231176browse

bitsCN.com


MySql查询随机几条数据

 

想到了  Max  RAND  这几个函数

用以下2种办法都可以实现查询。  速度还行。

 

几十万数据左右, 没有什么问题。

 

SELECT * FROM `news`

WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `news`))) 

LIMIT 10;

 

SELECT *

FROM `news` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `news`)-(SELECT MIN(id) FROM `news`))+(SELECT MIN(id) FROM `news`)) AS id) AS t2

WHERE t1.id >= t2.id

 LIMIT 10;

 

来源 http://blog.csdn.net/yjflinchong/article/details/8444417
 

bitsCN.com
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