{"id":5272,"date":"2024-11-14T09:15:29","date_gmt":"2024-11-14T01:15:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5272\/"},"modified":"2024-11-14T09:15:29","modified_gmt":"2024-11-14T01:15:29","slug":"mysql%e6%b8%b8%e6%a0%87%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5272\/","title":{"rendered":"mysql\u6e38\u6807\u600e\u4e48\u8bbe\u7f6e"},"content":{"rendered":"<blockquote><p>\n  \u8bbe\u7f6e mysql \u6e38\u6807\u7684\u6b65\u9aa4\uff1a1. \u4f7f\u7528 declare \u8bed\u53e5\u521b\u5efa\u6e38\u6807\u30022. \u4f7f\u7528 open \u8bed\u53e5\u6253\u5f00\u6e38\u6807\u30023. \u4f7f\u7528 fetch \u8bed\u53e5\u83b7\u53d6\u7ed3\u679c\u30024. \u4f7f\u7528 close \u8bed\u53e5\u5173\u95ed\u6e38\u6807\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052618064858140.jpg\" class=\"aligncenter\" title=\"mysql\u6e38\u6807\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" alt=\"mysql\u6e38\u6807\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u8bbe\u7f6e MySQL \u6e38\u6807<\/p>\n<p>MySQL \u6e38\u6807\u662f\u4e00\u79cd\u5728\u7ed3\u679c\u96c6\u4e0a\u8fdb\u884c\u904d\u5386\u7684\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u9010\u884c\u8bbf\u95ee\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u7ed3\u679c\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u8bbe\u7f6e\u6e38\u6807\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u6e38\u6807\uff1a<\/strong>\u4f7f\u7528 DECLARE \u8bed\u53e5\u521b\u5efa\u6e38\u6807\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>DECLARE cursor_name CURSOR FOR query;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>cursor_name \u662f\u6e38\u6807\u7684\u540d\u79f0\u3002<\/li>\n<li>query \u662f\u8981\u6267\u884c\u7684\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<ol>\n<li> <strong>\u6253\u5f00\u6e38\u6807\uff1a<\/strong>\u4f7f\u7528 OPEN \u8bed\u53e5\u6253\u5f00\u6e38\u6807\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>OPEN cursor_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u83b7\u53d6\u7ed3\u679c\uff1a<\/strong>\u4f7f\u7528 FETCH \u8bed\u53e5\u4ece\u6e38\u6807\u4e2d\u83b7\u53d6\u7ed3\u679c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>FETCH cursor_name INTO variable1, variable2, ...;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>variable1, variable2, &#8230; \u662f\u8981\u5b58\u50a8\u7ed3\u679c\u7684\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<ol>\n<li> <strong>\u5173\u95ed\u6e38\u6807\uff1a<\/strong>\u4f7f\u7528 CLOSE \u8bed\u53e5\u5173\u95ed\u6e38\u6807\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>CLOSE cursor_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8981\u8bbe\u7f6e\u4e00\u4e2a\u6e38\u6807\u6765\u904d\u5386 customers \u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>DECLARE customer_cursor CURSOR FOR SELECT * FROM customers;\nOPEN customer_cursor;\nFETCH customer_cursor INTO id, name, email;\n-- \u5bf9\u7ed3\u679c\u6267\u884c\u64cd\u4f5c\nCLOSE customer_cursor;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6e38\u6807\u600e\u4e48\u8bbe\u7f6e\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>\u8bbe\u7f6e mysql \u6e38\u6807\u7684\u6b65\u9aa4\uff1a1. \u4f7f\u7528 declare \u8bed\u53e5\u521b\u5efa\u6e38\u6807\u30022. \u4f7f\u7528 open \u8bed\u53e5\u6253\u5f00\u6e38\u6807\u30023. \u4f7f\u7528 fetch \u8bed\u53e5\u83b7\u53d6\u7ed3\u679c\u30024. \u4f7f\u7528 close \u8bed\u53e5\u5173\u95ed\u6e38\u6807\u3002 \u5982\u4f55\u8bbe\u7f6e MySQL \u6e38\u6807 MySQL \u6e38\u6807\u662f\u4e00\u79cd\u5728\u7ed3\u679c\u96c6\u4e0a\u8fdb\u884c\u904d\u5386\u7684\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u9010\u884c\u8bbf\u95ee\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u7ed3\u679c\u6267\u884c\u64cd\u4f5c\u3002 \u8bbe\u7f6e\u6e38\u6807\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a \u521b\u5efa\u6e38\u6807\uff1a\u4f7f\u7528 DECLARE \u8bed\u53e5\u521b\u5efa\u6e38\u6807\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a DECLARE cursor_name CURSOR FOR query; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a cursor_name \u662f\u6e38\u6807\u7684\u540d\u79f0\u3002 query \u662f\u8981\u6267\u884c\u7684\u67e5\u8be2\u3002 \u6253\u5f00\u6e38\u6807\uff1a\u4f7f\u7528 OPEN \u8bed\u53e5\u6253\u5f00\u6e38\u6807\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a OPEN cursor_name; \u767b\u5f55\u540e\u590d\u5236 \u83b7\u53d6\u7ed3\u679c\uff1a\u4f7f\u7528 FETCH \u8bed\u53e5\u4ece\u6e38\u6807\u4e2d\u83b7\u53d6\u7ed3\u679c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a FETCH cursor_name INTO variable1, variable2, &#8230;; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a variable1, variable2, &#8230; [&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-5272","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5272","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=5272"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5272\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}