{"id":6209,"date":"2024-11-14T09:45:13","date_gmt":"2024-11-14T01:45:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6209\/"},"modified":"2024-11-14T09:45:13","modified_gmt":"2024-11-14T01:45:13","slug":"mysql%e5%a6%82%e4%bd%95%e5%86%99%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6209\/","title":{"rendered":"mysql\u5982\u4f55\u5199\u51fd\u6570"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u521b\u5efa\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 create function \u8bed\u6cd5\uff0c\u8be5\u8bed\u6cd5\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u5217\u8868\u3001\u8fd4\u56de\u7c7b\u578b\u548c\u51fd\u6570\u4f53\uff0c\u51fd\u6570\u4f53\u5305\u542b\u51fd\u6570\u903b\u8f91\uff0c\u4f8b\u5982\u793a\u4f8b\u4e2d\u8fd4\u56de\u4e24\u4e2a\u6574\u6570\u548c\u7684\u51fd\u6570 get_sum\u3002\u51fd\u6570\u53ef\u4ee5\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528 call \u8bed\u53e5\u8c03\u7528\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/15\/2024061513332470256.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u5199\u51fd\u6570\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u5199\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u521b\u5efa\u51fd\u6570<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u79cd\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u53c2\u6570\u5e76\u8fd4\u56de\u5355\u4e2a\u503c\u7684\u9884\u5148\u5b9a\u4e49\u7684\u4ee3\u7801\u5757\u3002\u51fd\u6570\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u7b80\u5316\u590d\u6742\u67e5\u8be2\u3002<\/p>\n<p><strong>\u521b\u5efa\u51fd\u6570\u7684\u8bed\u6cd5<\/strong><\/p>\n<pre>CREATE FUNCTION \u51fd\u6570\u540d (\u53c2\u6570\u5217\u8868) RETURNS \u8fd4\u56de\u7c7b\u578b\nBEGIN\n  \u51fd\u6570\u4f53\nEND<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570\u5217\u8868\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b9a\u4e49\u51fd\u6570\u6240\u9700\u7684\u53c2\u6570\u53ca\u5176\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><strong>\u8fd4\u56de\u7c7b\u578b\uff1a<\/strong><\/p>\n<ul>\n<li>\u6307\u5b9a\u51fd\u6570\u8fd4\u56de\u7684\u503c\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><strong>\u51fd\u6570\u4f53\uff1a<\/strong><\/p>\n<ul>\n<li>\u5305\u542b\u51fd\u6570\u903b\u8f91\u7684 SQL \u8bed\u53e5\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a get_sum \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\uff1a<\/p>\n<pre>CREATE FUNCTION get_sum (a INT, b INT) RETURNS INT\nBEGIN\n  RETURN a + b;\nEND<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8c03\u7528\u51fd\u6570\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 CALL \u8bed\u53e5\u8c03\u7528\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\uff1a<\/p>\n<pre>SELECT get_sum(10, 20);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u51fd\u6570\u53ef\u4ee5\u662f\u786e\u5b9a\u6027\u7684\uff08\u8fd4\u56de\u503c\u4ec5\u53d6\u51b3\u4e8e\u8f93\u5165\u53c2\u6570\uff09\u6216\u975e\u786e\u5b9a\u6027\u7684\uff08\u8fd4\u56de\u503c\u8fd8\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u72b6\u6001\uff09\u3002<\/li>\n<li>MySQL \u652f\u6301\u591a\u79cd\u51fd\u6570\u7c7b\u578b\uff0c\u5305\u62ec\u6807\u91cf\u51fd\u6570\u3001\u8868\u51fd\u6570\u548c\u8fc7\u7a0b\u51fd\u6570\u3002<\/li>\n<li>\u51fd\u6570\u53ef\u4ee5\u5305\u542b\u63a7\u5236\u6d41\u8bed\u53e5\uff08\u5982 IF \u548c WHILE\uff09\u548c\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n<li>\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u6e38\u6807\u8fdb\u884c\u8fed\u4ee3\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5199\u51fd\u6570\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>\u5728 mysql \u4e2d\u521b\u5efa\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 create function \u8bed\u6cd5\uff0c\u8be5\u8bed\u6cd5\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u5217\u8868\u3001\u8fd4\u56de\u7c7b\u578b\u548c\u51fd\u6570\u4f53\uff0c\u51fd\u6570\u4f53\u5305\u542b\u51fd\u6570\u903b\u8f91\uff0c\u4f8b\u5982\u793a\u4f8b\u4e2d\u8fd4\u56de\u4e24\u4e2a\u6574\u6570\u548c\u7684\u51fd\u6570 get_sum\u3002\u51fd\u6570\u53ef\u4ee5\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528 call \u8bed\u53e5\u8c03\u7528\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u521b\u5efa\u51fd\u6570 \u5728 MySQL \u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u79cd\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u53c2\u6570\u5e76\u8fd4\u56de\u5355\u4e2a\u503c\u7684\u9884\u5148\u5b9a\u4e49\u7684\u4ee3\u7801\u5757\u3002\u51fd\u6570\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u7b80\u5316\u590d\u6742\u67e5\u8be2\u3002 \u521b\u5efa\u51fd\u6570\u7684\u8bed\u6cd5 CREATE FUNCTION \u51fd\u6570\u540d (\u53c2\u6570\u5217\u8868) RETURNS \u8fd4\u56de\u7c7b\u578b BEGIN \u51fd\u6570\u4f53 END \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\u5217\u8868\uff1a \u5b9a\u4e49\u51fd\u6570\u6240\u9700\u7684\u53c2\u6570\u53ca\u5176\u6570\u636e\u7c7b\u578b\u3002 \u8fd4\u56de\u7c7b\u578b\uff1a \u6307\u5b9a\u51fd\u6570\u8fd4\u56de\u7684\u503c\u7684\u6570\u636e\u7c7b\u578b\u3002 \u51fd\u6570\u4f53\uff1a \u5305\u542b\u51fd\u6570\u903b\u8f91\u7684 SQL \u8bed\u53e5\u3002 \u793a\u4f8b\uff1a \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a get_sum \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u578b\u53c2\u6570\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\uff1a CREATE FUNCTION get_sum (a INT, b INT) RETURNS INT BEGIN RETURN a + b; END \u767b\u5f55\u540e\u590d\u5236 \u8c03\u7528\u51fd\u6570\uff1a [&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-6209","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6209","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=6209"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6209\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}