Skip to content

Instantly share code, notes, and snippets.

@viko16
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viko16/329a0f5b18c59c0f1614 to your computer and use it in GitHub Desktop.
Save viko16/329a0f5b18c59c0f1614 to your computer and use it in GitHub Desktop.
正确的mysql随机查找语句 #sql #php
// 千万不要这样做:
$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