{"id":31517,"date":"2009-04-01T18:49:08","date_gmt":"2009-04-01T10:49:08","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=31517"},"modified":"2009-04-01T18:49:08","modified_gmt":"2009-04-01T10:49:08","slug":"mysql%e4%bd%bf%e7%94%a8rand%e9%9a%8f%e6%9c%ba%e6%9f%a5%e8%af%a2%e8%ae%b0%e5%bd%95%e6%95%88%e7%8e%87%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"http:\/\/www.fuhaijun.com\/?p=31517","title":{"rendered":"mysql\u4f7f\u7528rand\u968f\u673a\u67e5\u8be2\u8bb0\u5f55\u6548\u7387\u6d4b\u8bd5"},"content":{"rendered":"<p>\u4e00\u76f4\u4ee5\u4e3amysql\u968f\u673a\u67e5\u8be2\u51e0\u6761\u6570\u636e\uff0c\u5c31\u7528<\/p>\n<p>SELECT * FROM `table` ORDER BY RAND() LIMIT 5<\/p>\n<p>\u5c31\u53ef\u4ee5\u4e86\u3002    <br \/>\u4f46\u662f\u771f\u6b63\u6d4b\u8bd5\u4e00\u4e0b\u624d\u53d1\u73b0\u8fd9\u6837\u6548\u7387\u975e\u5e38\u4f4e\u3002\u4e00\u4e2a15\u4e07\u4f59\u6761\u7684\u5e93\uff0c\u67e5\u8be25\u6761\u6570\u636e\uff0c\u5c45\u7136\u89818\u79d2\u4ee5\u4e0a<\/p>\n<p>\u67e5\u770b\u5b98\u65b9\u624b\u518c\uff0c\u4e5f\u8bf4rand()\u653e\u5728ORDER BY \u5b50\u53e5\u4e2d\u4f1a\u88ab\u6267\u884c\u591a\u6b21\uff0c\u81ea\u7136\u6548\u7387\u53ca\u5f88\u4f4e\u3002<\/p>\n<blockquote>\n<p>You cannot use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times.<\/p>\n<\/blockquote>\n<p>\u641c\u7d22Google\uff0c\u7f51\u4e0a\u57fa\u672c\u4e0a\u90fd\u662f\u67e5\u8be2max(id) * rand()\u6765\u968f\u673a\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<p>SELECT *    <br \/>FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `table`)) AS id) AS t2     <br \/>WHERE t1.id &gt;= t2.id     <br \/>ORDER BY t1.id ASC LIMIT 5;<\/p>\n<p>\u4f46\u662f\u8fd9\u6837\u4f1a\u4ea7\u751f\u8fde\u7eed\u76845\u6761\u8bb0\u5f55\u3002\u89e3\u51b3\u529e\u6cd5\u53ea\u80fd\u662f\u6bcf\u6b21\u67e5\u8be2\u4e00\u6761\uff0c\u67e5\u8be25\u6b21\u3002\u5373\u4fbf\u5982\u6b64\u4e5f\u503c\u5f97\uff0c\u56e0\u4e3a15\u4e07\u6761\u7684\u8868\uff0c\u67e5\u8be2\u53ea\u9700\u89810.01\u79d2\u4e0d\u5230\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u8bed\u53e5\u91c7\u7528\u7684\u662fJOIN\uff0cmysql\u7684\u8bba\u575b\u4e0a\u6709\u4eba\u4f7f\u7528<\/p>\n<p>SELECT *    <br \/>FROM `table`     <br \/>WHERE id &gt;= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` )     <br \/>ORDER BY id LIMIT 1;<\/p>\n<p>\u6211\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u9700\u89810.5\u79d2\uff0c\u901f\u5ea6\u4e5f\u4e0d\u9519\uff0c\u4f46\u662f\u8ddf\u4e0a\u9762\u7684\u8bed\u53e5\u8fd8\u662f\u6709\u5f88\u5927\u5dee\u8ddd\u3002\u603b\u89c9\u6709\u4ec0\u4e48\u5730\u65b9\u4e0d\u6b63\u5e38\u3002<\/p>\n<p>\u4e8e\u662f\u6211\u628a\u8bed\u53e5\u6539\u5199\u4e86\u4e00\u4e0b\u3002<\/p>\n<p>SELECT * FROM `table`    <br \/>WHERE id &gt;= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table`)))\ufffd     <br \/>ORDER BY id LIMIT 1;<\/p>\n<p>\u8fd9\u4e0b\uff0c\u6548\u7387\u53c8\u63d0\u9ad8\u4e86\uff0c\u67e5\u8be2\u65f6\u95f4\u53ea\u67090.01\u79d2<\/p>\n<p>\u6700\u540e\uff0c\u518d\u628a\u8bed\u53e5\u5b8c\u5584\u4e00\u4e0b\uff0c\u52a0\u4e0aMIN(id)\u7684\u5224\u65ad\u3002\u6211\u5728\u6700\u5f00\u59cb\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u5c31\u662f\u56e0\u4e3a\u6ca1\u6709\u52a0\u4e0aMIN(id)\u7684\u5224\u65ad\uff0c\u7ed3\u679c\u6709\u4e00\u534a\u7684\u65f6\u95f4\u603b\u662f\u67e5\u8be2\u5230\u8868\u4e2d\u7684\u524d\u9762\u51e0\u884c\u3002    <br \/>\u5b8c\u6574\u67e5\u8be2\u8bed\u53e5\u662f\uff1a<\/p>\n<p>SELECT * FROM `table`    <br \/>WHERE id &gt;= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`)) + (SELECT MIN(id) FROM `table`)))\ufffd     <br \/>ORDER BY id LIMIT 1;<\/p>\n<p>SELECT *    <br \/>FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+(SELECT MIN(id) FROM `table`)) AS id) AS t2     <br \/>WHERE t1.id &gt;= t2.id     <br \/>ORDER BY t1.id LIMIT 1;<\/p>\n<p>\u6700\u540e\u5728php\u4e2d\u5bf9\u8fd9\u4e24\u4e2a\u8bed\u53e5\u8fdb\u884c\u5206\u522b\u67e5\u8be210\u6b21\uff0c    <br \/>\u524d\u8005\u82b1\u8d39\u65f6\u95f4 0.147433 \u79d2     <br \/>\u540e\u8005\u82b1\u8d39\u65f6\u95f4 0.015130 \u79d2     <br \/>\u770b\u6765\u91c7\u7528JOIN\u7684\u8bed\u6cd5\u6bd4\u76f4\u63a5\u5728WHERE\u4e2d\u4f7f\u7528\u51fd\u6570\u6548\u7387\u8fd8\u8981\u9ad8\u5f88\u591a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u76f4\u4ee5\u4e3amysql\u968f\u673a\u67e5\u8be2\u51e0\u6761\u6570\u636e\uff0c\u5c31\u7528 SELECT * FROM `table` ORDER BY RAN [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-31517","post","type-post","status-publish","format-standard","hentry","category-default"],"_links":{"self":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/31517"}],"collection":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=31517"}],"version-history":[{"count":0,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/31517\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=31517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=31517"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=31517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}