{"id":6153,"date":"2024-11-14T08:24:41","date_gmt":"2024-11-14T00:24:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6153\/"},"modified":"2024-11-14T08:24:41","modified_gmt":"2024-11-14T00:24:41","slug":"mysql%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6153\/","title":{"rendered":"mysql\u5982\u4f55\u521b\u5efa\u51fd\u6570"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u521b\u5efa\u51fd\u6570\u7684\u6b65\u9aa4\u5305\u62ec\uff1a1. \u786e\u5b9a\u51fd\u6570\u540d\u79f0\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\uff1b2. \u4f7f\u7528 create function \u8bed\u53e5\u521b\u5efa\u51fd\u6570\uff1b3. \u5728 begin \u548c end \u4e4b\u95f4\u7f16\u5199\u51fd\u6570\u4f53\uff1b4. \u4f7f\u7528 call \u8bed\u53e5\u8c03\u7528\u51fd\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/14\/2024061403003325836.jpg\" class=\"aligncenter\" title=\"mysql\u5982\u4f55\u521b\u5efa\u51fd\u6570\u63d2\u56fe\" alt=\"mysql\u5982\u4f55\u521b\u5efa\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d\u7684\u51fd\u6570\u521b\u5efa<\/strong><\/p>\n<p><strong>\u521b\u5efa\u51fd\u6570\u7684\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>CREATE FUNCTION function_name (parameter_list) RETURNS return_type\nAS\nBEGIN\n  -- \u51fd\u6570\u4f53\nEND<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>function_name\uff1a<\/strong> \u51fd\u6570\u7684\u540d\u79f0<\/li>\n<li> <strong>parameter_list\uff1a<\/strong> \u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\uff0c\u6bcf\u4e2a\u53c2\u6570\u5305\u542b\u5176\u6570\u636e\u7c7b\u578b<\/li>\n<li> <strong>return_type\uff1a<\/strong> \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b<\/li>\n<li> <strong>BEGIN &#8230; END\uff1a<\/strong> \u51fd\u6570\u4f53\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u6807\u5fd7<\/li>\n<\/ul>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\u786e\u5b9a\u51fd\u6570\u7684\u540d\u79f0\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002<\/li>\n<li>\u4f7f\u7528 CREATE FUNCTION \u8bed\u53e5\u521b\u5efa\u51fd\u6570\u3002<\/li>\n<li>\u5728 BEGIN \u548c END \u4e4b\u95f4\u7f16\u5199\u51fd\u6570\u4f53\uff0c\u5305\u62ec\u51fd\u6570\u7684\u903b\u8f91\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a add_numbers \u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u6574\u6570\u53c2\u6570\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\uff1a<\/p>\n<pre>CREATE FUNCTION add_numbers (num1 INT, num2 INT) RETURNS INT\nAS\nBEGIN\n  DECLARE result INT;\n  SET result = num1 + num2;\n  RETURN result;\nEND<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528 CALL \u8bed\u53e5\u8c03\u7528\u51fd\u6570\uff1a<\/p>\n<pre>SELECT add_numbers(10, 20); -- \u8f93\u51fa\uff1a30<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u51fd\u6570\u540d\u79f0\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002<\/li>\n<li>\u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u4f55 MySQL \u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5fc5\u987b\u4e0e\u51fd\u6570\u4f53\u4e2d\u8fd4\u56de\u7684\u503c\u7c7b\u578b\u5339\u914d\u3002<\/li>\n<li>\u51fd\u6570\u4f53\u53ef\u4ee5\u4f7f\u7528 MySQL \u8bed\u8a00\u7684\u6240\u6709\u529f\u80fd\uff0c\u5305\u62ec\u53d8\u91cf\u58f0\u660e\u3001\u63a7\u5236\u6d41\u8bed\u53e5\u548c\u51fd\u6570\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u521b\u5efa\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\u7684\u6b65\u9aa4\u5305\u62ec\uff1a1. \u786e\u5b9a\u51fd\u6570\u540d\u79f0\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\uff1b2. \u4f7f\u7528 create function \u8bed\u53e5\u521b\u5efa\u51fd\u6570\uff1b3. \u5728 begin \u548c end \u4e4b\u95f4\u7f16\u5199\u51fd\u6570\u4f53\uff1b4. \u4f7f\u7528 call \u8bed\u53e5\u8c03\u7528\u51fd\u6570\u3002 MySQL \u4e2d\u7684\u51fd\u6570\u521b\u5efa \u521b\u5efa\u51fd\u6570\u7684\u8bed\u6cd5\uff1a CREATE FUNCTION function_name (parameter_list) RETURNS return_type AS BEGIN &#8212; \u51fd\u6570\u4f53 END \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\uff1a function_name\uff1a \u51fd\u6570\u7684\u540d\u79f0 parameter_list\uff1a \u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\uff0c\u6bcf\u4e2a\u53c2\u6570\u5305\u542b\u5176\u6570\u636e\u7c7b\u578b return_type\uff1a \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b BEGIN &#8230; END\uff1a \u51fd\u6570\u4f53\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u6807\u5fd7 \u6b65\u9aa4\uff1a \u786e\u5b9a\u51fd\u6570\u7684\u540d\u79f0\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002 \u4f7f\u7528 CREATE FUNCTION \u8bed\u53e5\u521b\u5efa\u51fd\u6570\u3002 \u5728 BEGIN \u548c END \u4e4b\u95f4\u7f16\u5199\u51fd\u6570\u4f53\uff0c\u5305\u62ec\u51fd\u6570\u7684\u903b\u8f91\u3002 \u793a\u4f8b\uff1a \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a [&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-6153","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6153","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=6153"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6153\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}