{"id":5728,"date":"2024-11-14T10:07:58","date_gmt":"2024-11-14T02:07:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5728\/"},"modified":"2024-11-14T10:07:58","modified_gmt":"2024-11-14T02:07:58","slug":"sql%e9%9a%8f%e6%9c%ba%e6%95%b0%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5728\/","title":{"rendered":"sql\u968f\u673a\u6570\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u5728 sql \u4e2d\u751f\u6210\u968f\u673a\u6570\uff1f\u4f7f\u7528 rand() \u51fd\u6570\u751f\u6210\u4ecb\u4e8e 0 \u548c 1 \u4e4b\u95f4\u7684\u4f2a\u968f\u673a\u6d6e\u70b9\u6570\u3002\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528 rand() \u51fd\u6570\u6765\u751f\u6210\u6574\u6570\u968f\u673a\u6570\uff0c\u4f8b\u5982\u4f7f\u7528 floor() \u51fd\u6570\u3002rand() \u51fd\u6570\u8fd8\u53ef\u4ee5\u751f\u6210\u4ecb\u4e8e\u4e24\u4e2a\u503c\u4e4b\u95f4\u7684\u968f\u673a\u6570\u6216\u5e03\u5c14\u503c\u3002\u6ce8\u610f rand() \u51fd\u6570\u751f\u6210\u7684\u662f\u4f2a\u968f\u673a\u6570\uff0c\u5e76\u4e14\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053021310078748.jpg\" class=\"aligncenter\" title=\"sql\u968f\u673a\u6570\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"sql\u968f\u673a\u6570\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 SQL \u4e2d\u751f\u6210\u968f\u673a\u6570<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\u751f\u6210\u968f\u673a\u6570\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528 RAND() \u51fd\u6570\u3002\u8be5\u51fd\u6570\u5c06\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e 0\uff08\u542b\uff09\u548c 1\uff08\u4e0d\u542b\uff09\u4e4b\u95f4\u7684\u4f2a\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<p>\u8981\u751f\u6210\u4e00\u4e2a\u6574\u6570\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 RAND() \u51fd\u6570\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u751f\u6210\u4ecb\u4e8e 1 \u548c 100 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff1a<\/strong><\/p>\n<pre>SELECT FLOOR(RAND() * 100) + 1;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u751f\u6210\u4ecb\u4e8e 0 \u548c 9999 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff1a<\/strong><\/p>\n<pre>SELECT FLOOR(RAND() * 9999);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ul>\n<p>RAND() \u51fd\u6570\u8fd8\u53ef\u7528\u4e8e\u751f\u6210\u5176\u4ed6\u7c7b\u578b\u7684\u968f\u673a\u6570\u636e\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e\u4e24\u4e2a\u503c\u4e4b\u95f4\u7684\u968f\u673a\u6570\uff1a<\/strong><\/p>\n<pre>SELECT RAND() * (MAX_VALUE - MIN_VALUE) + MIN_VALUE;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u751f\u6210\u4e00\u4e2a\u5e03\u5c14\u503c\uff08True\/False\uff09\uff1a<\/strong><\/p>\n<pre>SELECT CASE WHEN RAND() &lt; 0.5 THEN True ELSE False END;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ul>\n<p><strong>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>RAND() \u51fd\u6570\u751f\u6210\u7684\u662f\u4f2a\u968f\u673a\u6570\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u662f\u7531\u7b97\u6cd5\u4ea7\u751f\u7684\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u7684\u968f\u673a\u6570\u3002<\/li>\n<li>RAND() \u51fd\u6570\u662f\u4e00\u4e2a\u4e0d\u53ef\u786e\u5b9a\u7684\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002<\/li>\n<li>\u5728\u67d0\u4e9b\u6570\u636e\u5e93\u4e2d\uff0cRAND() \u51fd\u6570\u53ef\u80fd\u6709\u4e00\u4e2a\u79cd\u5b50\u503c\uff0c\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u968f\u673a\u6570\u5e8f\u5217\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u968f\u673a\u6570\u600e\u4e48\u5199\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>\u5982\u4f55\u5728 sql \u4e2d\u751f\u6210\u968f\u673a\u6570\uff1f\u4f7f\u7528 rand() \u51fd\u6570\u751f\u6210\u4ecb\u4e8e 0 \u548c 1 \u4e4b\u95f4\u7684\u4f2a\u968f\u673a\u6d6e\u70b9\u6570\u3002\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528 rand() \u51fd\u6570\u6765\u751f\u6210\u6574\u6570\u968f\u673a\u6570\uff0c\u4f8b\u5982\u4f7f\u7528 floor() \u51fd\u6570\u3002rand() \u51fd\u6570\u8fd8\u53ef\u4ee5\u751f\u6210\u4ecb\u4e8e\u4e24\u4e2a\u503c\u4e4b\u95f4\u7684\u968f\u673a\u6570\u6216\u5e03\u5c14\u503c\u3002\u6ce8\u610f rand() \u51fd\u6570\u751f\u6210\u7684\u662f\u4f2a\u968f\u673a\u6570\uff0c\u5e76\u4e14\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\u3002 \u5982\u4f55\u5728 SQL \u4e2d\u751f\u6210\u968f\u673a\u6570 \u5728 SQL \u4e2d\u751f\u6210\u968f\u673a\u6570\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528 RAND() \u51fd\u6570\u3002\u8be5\u51fd\u6570\u5c06\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e 0\uff08\u542b\uff09\u548c 1\uff08\u4e0d\u542b\uff09\u4e4b\u95f4\u7684\u4f2a\u968f\u673a\u6d6e\u70b9\u6570\u3002 \u8981\u751f\u6210\u4e00\u4e2a\u6574\u6570\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 RAND() \u51fd\u6570\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff1a \u751f\u6210\u4ecb\u4e8e 1 \u548c 100 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff1a SELECT FLOOR(RAND() * 100) + 1; \u767b\u5f55\u540e\u590d\u5236 \u751f\u6210\u4ecb\u4e8e 0 \u548c 9999 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff1a SELECT FLOOR(RAND() * 9999); \u767b\u5f55\u540e\u590d\u5236 RAND() \u51fd\u6570\u8fd8\u53ef\u7528\u4e8e\u751f\u6210\u5176\u4ed6\u7c7b\u578b\u7684\u968f\u673a\u6570\u636e\uff0c\u4f8b\u5982\uff1a \u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e\u4e24\u4e2a\u503c\u4e4b\u95f4\u7684\u968f\u673a\u6570\uff1a SELECT [&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-5728","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5728","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=5728"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5728\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}