{"id":80270,"date":"2024-10-16T14:06:14","date_gmt":"2024-10-16T06:06:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/80270\/"},"modified":"2024-10-16T14:06:14","modified_gmt":"2024-10-16T06:06:14","slug":"mysql%e6%8c%87%e4%bb%a4-while%ef%bc%88%e5%be%aa%e7%92%b0%e8%aa%9e%e5%8f%a5%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/80270\/","title":{"rendered":"MySQL\u6307\u4ee4: WHILE\uff08\u5faa\u74b0\u8a9e\u53e5\uff09"},"content":{"rendered":"<h1 id=\"mysql%e6%8c%87%e4%bb%a4-while%ef%bc%88%e5%be%aa%e7%92%b0%e8%aa%9e%e5%8f%a5%ef%bc%89-LKeGwIfiay\">MySQL\u6307\u4ee4: WHILE\uff08\u5faa\u74b0\u8a9e\u53e5\uff09<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cMySQL\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u806f\u5f0f\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u7a2e\u529f\u80fd\u4f86\u8655\u7406\u8cc7\u6599\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u5faa\u74b0\u8a9e\u53e5\u3002WHILE\u5faa\u74b0\u8a9e\u53e5\u5141\u8a31\u7528\u6236\u5728\u6eff\u8db3\u7279\u5b9a\u689d\u4ef6\u7684\u60c5\u6cc1\u4e0b\u91cd\u8907\u57f7\u884c\u4e00\u6bb5\u7a0b\u5f0f\u78bc\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u4e2d\u7684WHILE\u5faa\u74b0\u8a9e\u53e5\uff0c\u5305\u62ec\u5176\u8a9e\u6cd5\u3001\u4f7f\u7528\u60c5\u5883\u53ca\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"while%e5%be%aa%e7%92%b0%e8%aa%9e%e5%8f%a5%e7%9a%84%e8%aa%9e%e6%b3%95-LKeGwIfiay\">WHILE\u5faa\u74b0\u8a9e\u53e5\u7684\u8a9e\u6cd5<\/h2>\n<p>WHILE\u5faa\u74b0\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>\nWHILE \u689d\u4ef6 DO\n    -- \u57f7\u884c\u7684SQL\u8a9e\u53e5\nEND WHILE;\n<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u689d\u4ef6\u662f\u4e00\u500b\u5e03\u6797\u8868\u9054\u5f0f\uff0c\u7576\u5176\u70ba\u771f\u6642\uff0c\u5faa\u74b0\u5c07\u7e7c\u7e8c\u57f7\u884c\u3002\u7576\u689d\u4ef6\u70ba\u5047\u6642\uff0c\u5faa\u74b0\u5c07\u7d42\u6b62\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e6%83%85%e5%a2%83-LKeGwIfiay\">\u4f7f\u7528\u60c5\u5883<\/h2>\n<p>WHILE\u5faa\u74b0\u901a\u5e38\u7528\u65bc\u9700\u8981\u91cd\u8907\u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\u7684\u60c5\u6cc1\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u8a08\u7b97\u7e3d\u548c\u6216\u5e73\u5747\u503c<\/li>\n<li>\u6279\u91cf\u66f4\u65b0\u8cc7\u6599<\/li>\n<li>\u6839\u64da\u67d0\u4e9b\u689d\u4ef6\u751f\u6210\u5831\u544a<\/li>\n<\/ul>\n<h2 id=\"%e7%af%84%e4%be%8b-LKeGwIfiay\">\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528WHILE\u5faa\u74b0\u7684\u7bc4\u4f8b\uff0c\u8a72\u7bc4\u4f8b\u8a08\u7b97\u5f9e1\u523010\u7684\u7e3d\u548c\uff1a<\/p>\n<pre><code>\nDELIMITER \/\/\n\nCREATE PROCEDURE CalculateSum()\nBEGIN\n    DECLARE total INT DEFAULT 0;\n    DECLARE counter INT DEFAULT 1;\n\n    WHILE counter &lt;= 10 DO\n        SET total = total + counter;\n        SET counter = counter + 1;\n    END WHILE;\n\n    SELECT total AS TotalSum;\nEND \/\/\n\nDELIMITER ;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70baCalculateSum\u7684\u5b58\u5132\u904e\u7a0b\u3002\u5b83\u4f7f\u7528WHILE\u5faa\u74b0\u5f9e1\u52a0\u523010\uff0c\u6700\u7d42\u8fd4\u56de\u7e3d\u548c\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-LKeGwIfiay\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528WHILE\u5faa\u74b0\u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u5faa\u74b0\u689d\u4ef6\u6700\u7d42\u6703\u8b8a\u70ba\u5047\uff0c\u5426\u5247\u5c07\u5c0e\u81f4\u7121\u9650\u5faa\u74b0\u3002<\/li>\n<li>\u5728\u5faa\u74b0\u5167\u90e8\u9032\u884c\u9069\u7576\u7684\u8b8a\u6578\u66f4\u65b0\uff0c\u4ee5\u907f\u514d\u7121\u9650\u5faa\u74b0\u7684\u60c5\u6cc1\u3002<\/li>\n<li>\u904e\u5ea6\u4f7f\u7528WHILE\u5faa\u74b0\u53ef\u80fd\u6703\u5f71\u97ff\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u8cc7\u6599\u6642\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-LKeGwIfiay\">\u7e3d\u7d50<\/h2>\n<p>WHILE\u5faa\u74b0\u8a9e\u53e5\u5728MySQL\u4e2d\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u5728\u6eff\u8db3\u7279\u5b9a\u689d\u4ef6\u7684\u60c5\u6cc1\u4e0b\u91cd\u8907\u57f7\u884cSQL\u8a9e\u53e5\u3002\u900f\u904e\u9069\u7576\u7684\u4f7f\u7528\uff0cWHILE\u5faa\u74b0\u53ef\u4ee5\u7c21\u5316\u8a31\u591a\u8907\u96dc\u7684\u8cc7\u6599\u8655\u7406\u4efb\u52d9\u3002\u82e5\u60a8\u5c0d\u65bc\u8cc7\u6599\u5eab\u7ba1\u7406\u6709\u9032\u4e00\u6b65\u7684\u9700\u6c42\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u63d0\u5347\u60a8\u7684\u8cc7\u6599\u5eab\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002\u7121\u8ad6\u662f\u9078\u64c7<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u9084\u662f\u5176\u4ed6\u89e3\u6c7a\u65b9\u6848\uff0cServer.HK\u90fd\u80fd\u63d0\u4f9b\u60a8\u6240\u9700\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u4e2d\u7684WHILE\u5faa\u74b0\u8a9e\u53e5\uff0c\u5b78\u7fd2\u5982\u4f55\u5728\u67e5\u8a62\u4e2d\u5be6\u73fe\u91cd\u8907\u57f7\u884c\u64cd\u4f5c\u4ee5\u63d0\u9ad8\u6578\u64da\u8655\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-80270","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=80270"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80270\/revisions"}],"predecessor-version":[{"id":80271,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80270\/revisions\/80271"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=80270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=80270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=80270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}