Home > Database > Mysql Tutorial > MySQL 查询随机条记录的sql语句和php计算概率

MySQL 查询随机条记录的sql语句和php计算概率

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:11:13
Original
1027 people have browsed it

最近在网上找了下mysql查询随机的几个sql,我把最终的记录下来。 SELECT * FROM uchome_mtag AS a JOIN (SELECT MAX(tagid) AS i

最近在网上找了下mysql查询随机的几个sql,我把最终的记录下来。
SELECT * FROM uchome_mtag AS a JOIN (SELECT MAX(tagid) AS id FROM uchome_mtag) AS b
ON (a.tagid>=FLOOR(b.id*RAND())) LIMIT 50
我试验后发现一个问题,当你的表里的总数和想要得到的条数很接近时,,可能会不理想,有可能你有10条,你想查出随机的8条时,却只给出了5条的结果。 应该是那个大于等于造成的吧。

还有php算概率的,也整理在这里吧。

100%    if(true)
1/2    if( rand(0,1) === 0 )
1/3    if( rand(0, 2) === 0 )
1/x    if( rand(0, x-1) === 0 )
x%     if( rand(0, 99) x‰    if( rand(0,999) /**

* 以$x/$y的概率返回true
*
* @param int $x
* @param int $y
* @return bool
*
* prob(1,2) = 1/2
* prob(1,100) = 1/100
*
*/
function prob($x, $y){
return ( mt_rand(0, $y-1) }

linux

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