{"id":5662,"date":"2024-11-14T08:29:13","date_gmt":"2024-11-14T00:29:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5662\/"},"modified":"2024-11-14T08:29:13","modified_gmt":"2024-11-14T00:29:13","slug":"sql%e5%be%aa%e7%8e%af%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5662\/","title":{"rendered":"sql\u5faa\u73af\u8bed\u53e5\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<blockquote><p>\n  sql\u5faa\u73af\u8bed\u53e5\u53ef\u91cd\u590d\u6267\u884c\u4ee3\u7801\uff0c\u76f4\u5230\u6761\u4ef6\u6ee1\u8db3\u3002\u5e38\u7528\u7684\u5faa\u73af\u5305\u62ec\uff1awhile\u5faa\u73af\uff1a\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u3002repeat\u5faa\u73af\uff1a\u5148\u6267\u884c\u4ee3\u7801\uff0c\u6761\u4ef6\u4e3a\u5047\u65f6\u91cd\u590d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053020095661977.jpg\" class=\"aligncenter\" title=\"sql\u5faa\u73af\u8bed\u53e5\u600e\u4e48\u4f7f\u7528\u63d2\u56fe\" alt=\"sql\u5faa\u73af\u8bed\u53e5\u600e\u4e48\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u5faa\u73af\u8bed\u53e5\u7684\u7528\u6cd5<\/strong><\/p>\n<p>\u5faa\u73af\u8bed\u53e5\u5728 SQL \u4e2d\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u3002\u5e38\u7528\u7684 SQL \u5faa\u73af\u8bed\u53e5\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>WHILE \u5faa\u73af\uff1a<\/strong>\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002<\/li>\n<li> <strong>REPEAT \u5faa\u73af\uff1a<\/strong>\u5148\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\uff0c\u7136\u540e\u68c0\u67e5\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u4e3a\u5047\uff0c\u5219\u91cd\u590d\u6267\u884c\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<p><strong>WHILE \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>WHILE &lt;condition&gt; DO\n  -- \u8981\u91cd\u590d\u6267\u884c\u7684\u4ee3\u7801\nEND WHILE;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>REPEAT \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>REPEAT\n  -- \u8981\u91cd\u590d\u6267\u884c\u7684\u4ee3\u7801\nUNTIL &lt;condition&gt;;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><strong>\u793a\u4f8b 1\uff1a\u4f7f\u7528 WHILE \u5faa\u73af\u6253\u5370\u4ece 1 \u5230 10 \u7684\u6570\u5b57<\/strong><\/p>\n<pre>DECLARE i INT DEFAULT 1;\n\nWHILE i &lt;= 10 DO\n  PRINT i;\n  SET i = i + 1;\nEND WHILE;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b 2\uff1a\u4f7f\u7528 REPEAT \u5faa\u73af\u7edf\u8ba1\u4e00\u5f20\u8868\u4e2d\u7684\u8bb0\u5f55\u6570<\/strong><\/p>\n<pre>DECLARE count INT DEFAULT 0;\n\nREPEAT\n  SELECT COUNT(*) INTO count FROM table_name;\n  UNTIL count &gt; 0;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u5faa\u73af\u6709\u7ed3\u675f\u6761\u4ef6\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65e0\u9650\u5faa\u73af\u3002<\/li>\n<li>\u5728\u5faa\u73af\u4e2d\u9002\u5f53\u66f4\u65b0\u53d8\u91cf\uff0c\u907f\u514d\u6b7b\u5faa\u73af\u3002<\/li>\n<li>\u5faa\u73af\u8bed\u53e5\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5faa\u73af\u8bed\u53e5\u600e\u4e48\u4f7f\u7528\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>sql\u5faa\u73af\u8bed\u53e5\u53ef\u91cd\u590d\u6267\u884c\u4ee3\u7801\uff0c\u76f4\u5230\u6761\u4ef6\u6ee1\u8db3\u3002\u5e38\u7528\u7684\u5faa\u73af\u5305\u62ec\uff1awhile\u5faa\u73af\uff1a\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u3002repeat\u5faa\u73af\uff1a\u5148\u6267\u884c\u4ee3\u7801\uff0c\u6761\u4ef6\u4e3a\u5047\u65f6\u91cd\u590d\u3002 SQL \u5faa\u73af\u8bed\u53e5\u7684\u7528\u6cd5 \u5faa\u73af\u8bed\u53e5\u5728 SQL \u4e2d\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u3002\u5e38\u7528\u7684 SQL \u5faa\u73af\u8bed\u53e5\u5305\u62ec\uff1a WHILE \u5faa\u73af\uff1a\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002 REPEAT \u5faa\u73af\uff1a\u5148\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\uff0c\u7136\u540e\u68c0\u67e5\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u4e3a\u5047\uff0c\u5219\u91cd\u590d\u6267\u884c\u4ee3\u7801\u3002 \u8bed\u6cd5\uff1a WHILE \u5faa\u73af\uff1a WHILE &lt;condition&gt; DO &#8212; \u8981\u91cd\u590d\u6267\u884c\u7684\u4ee3\u7801 END WHILE; \u767b\u5f55\u540e\u590d\u5236 REPEAT \u5faa\u73af\uff1a REPEAT &#8212; \u8981\u91cd\u590d\u6267\u884c\u7684\u4ee3\u7801 UNTIL &lt;condition&gt;; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u793a\u4f8b\uff1a \u793a\u4f8b 1\uff1a\u4f7f\u7528 WHILE \u5faa\u73af\u6253\u5370\u4ece 1 \u5230 10 \u7684\u6570\u5b57 DECLARE i INT DEFAULT 1; WHILE i &lt;= 10 DO PRINT i; SET i = [&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-5662","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5662","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=5662"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5662\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}