SQL statement used by MySQL to query random records and php computing Probability

Source: Internet
Author: User
Recently, I found several random SQL queries in mysql on the Internet. I recorded the results. SELECT * FROMuchome_mtagASaJOIN (SELECTMAX (tagid) ASi

Recently, I found several random SQL queries in mysql on the Internet. I recorded the results. SELECT * FROM uchome_mtag AS a JOIN (select max (tagid) AS I

Recently, I found several random SQL queries in mysql on the Internet. I recorded the results.
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
I found a problem after the experiment. When the total number in your table is very close to the number of items you want, it may be unsatisfactory. Maybe you have 10, when you want to find 8 random results, only 5 results are given. It should be the one that is greater than or equal to the cause.

There is also a php probability calculation. Let's sort it out here.

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)
X ‰ if (rand (0,999) <x)
/**

* Returns true based on the probability of $ x/$ y.
*
* @ Param int $ x
* @ Param int $ y
* @ Return bool
*
* Prob (1/2) =
* Prob (1,100) = 1/100
*
*/
Function prob ($ x, $ y ){
Return (mt_rand (0, $ Y-1) <$ x );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.