Last active
August 29, 2015 14:01
-
-
Save viko16/329a0f5b18c59c0f1614 to your computer and use it in GitHub Desktop.
正确的mysql随机查找语句 #sql #php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 千万不要这样做: | |
$r = mysql_query("SELECT username FROM user ORDER BY RAND() LIMIT 1"); | |
// 这要会更好: | |
$r = mysql_query("SELECT count(*) FROM user"); | |
$d = mysql_fetch_row($r); | |
$rand = mt_rand(0,$d[0] - 1); | |
$r = mysql_query("SELECT username FROM user LIMIT $rand, 1"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment