{"id":6754,"date":"2024-11-14T09:53:40","date_gmt":"2024-11-14T01:53:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6754\/"},"modified":"2024-11-14T09:53:40","modified_gmt":"2024-11-14T01:53:40","slug":"%e5%a6%82%e4%bd%95%e6%89%93%e4%b9%b1-mysql-%e8%a1%a8%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae%e9%a1%ba%e5%ba%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6754\/","title":{"rendered":"\u5982\u4f55\u6253\u4e71 MySQL \u8868\u4e2d\u7684\u6570\u636e\u987a\u5e8f\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172990089015840.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u6253\u4e71 MySQL \u8868\u4e2d\u7684\u6570\u636e\u987a\u5e8f\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u6253\u4e71 MySQL \u8868\u4e2d\u7684\u6570\u636e\u987a\u5e8f\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6253\u4e71  \u8868\u4e2d\u7684\u6570\u636e\u987a\u5e8f<\/strong><\/p>\n<p>\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8868\u4e2d\u7684\u6570\u636e\u91cd\u65b0\u6210\u968f\u673a\u987a\u5e8f\uff0c\u4ee5\u6d88\u9664\u6309\u63d2\u5165\u987a\u5e8f\u6392\u5217\u7684\u504f\u597d\u3002\u5728 mysql \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<p><strong>\u4f7f\u7528 join \u548c rand() \u51fd\u6570<\/strong><\/p>\n<p>\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 join \u548c rand() \u51fd\u6570\u3002\u6b64\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u8868\uff0c\u5176\u4e2d\u5305\u542b\u9650\u5236\u4e3a\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6392\u5217\u7684 id\u3002\u7136\u540e\uff0c\u4f7f\u7528 join \u5c06\u539f\u59cb\u8868\u4e0e\u4e34\u65f6\u8868\u8fde\u63a5\uff0c\u6839\u636e\u968f\u673a\u5316\u7684 id \u7b5b\u9009\u51fa\u8bb0\u5f55\u3002<\/p>\n<pre>select *\nfrom test1 as u\ninner join (select id from test1 where totalscore between 5 and 100 order by rand() limit 100) as t on t.id=u.id\nwhere 1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 order by rand()<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 order by rand() \u5b50\u53e5\u3002\u6b64\u65b9\u6cd5\u76f4\u63a5\u5bf9\u539f\u59cb\u8868\u8fdb\u884c\u968f\u673a\u6392\u5e8f\uff0c\u4f46\u7531\u4e8e mysql \u4e2d\u5404\u79cd\u539f\u56e0\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u53ef\u80fd\u8f83\u5dee\u3002<\/p>\n<pre>SELECT * FROM student WHERE totalScore BETWEEN 5 AND 100 ORDER BY RAND() LIMIT 100;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u7ec8\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u8868\u7684\u5927\u5c0f\u3001\u6570\u636e\u8303\u56f4\u548c\u6240\u9700\u7684\u6027\u80fd\u6c34\u5e73\u3002\u5bf9\u4e8e\u5927\u8868\u6216\u9700\u8981\u9ad8\u6027\u80fd\u7684\u573a\u666f\uff0c\u5efa\u8bae\u4f7f\u7528 join \u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u6253\u4e71 MySQL \u8868\u4e2d\u7684\u6570\u636e\u987a\u5e8f\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6253\u4e71 \u8868\u4e2d\u7684\u6570\u636e\u987a\u5e8f \u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8868\u4e2d\u7684\u6570\u636e\u91cd\u65b0\u6210\u968f\u673a\u987a\u5e8f\uff0c\u4ee5\u6d88\u9664\u6309\u63d2\u5165\u987a\u5e8f\u6392\u5217\u7684\u504f\u597d\u3002\u5728 mysql \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002 \u4f7f\u7528 join \u548c rand() \u51fd\u6570 \u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 join \u548c rand() \u51fd\u6570\u3002\u6b64\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u8868\uff0c\u5176\u4e2d\u5305\u542b\u9650\u5236\u4e3a\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6392\u5217\u7684 id\u3002\u7136\u540e\uff0c\u4f7f\u7528 join \u5c06\u539f\u59cb\u8868\u4e0e\u4e34\u65f6\u8868\u8fde\u63a5\uff0c\u6839\u636e\u968f\u673a\u5316\u7684 id \u7b5b\u9009\u51fa\u8bb0\u5f55\u3002 select * from test1 as u inner join (select id from test1 where totalscore between 5 and 100 order by rand() limit 100) as t on t.id=u.id where 1 \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 order by rand() \u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6754","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6754"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6754\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}