{"id":175497,"date":"2024-11-08T06:22:16","date_gmt":"2024-11-07T22:22:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/175497\/"},"modified":"2024-11-08T06:22:17","modified_gmt":"2024-11-07T22:22:17","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e8%aa%bf%e7%94%a8%e5%87%bd%e6%95%b8%e5%84%aa%e5%8c%96%e6%95%b8%e6%93%9a%e6%9f%a5%e8%a9%a2%ef%bc%9f-%e6%95%b8%e6%93%9a%e5%ba%ab%e8%aa%bf","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/175497\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\u512a\u5316\u6578\u64da\u67e5\u8a62\uff1f (\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e8%aa%bf%e7%94%a8%e5%87%bd%e6%95%b8%e5%84%aa%e5%8c%96%e6%95%b8%e6%93%9a%e6%9f%a5%e8%a9%a2%ef%bc%9f-CmpTlFoUAn\">\u5982\u4f55\u4f7f\u7528\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\u512a\u5316\u6578\u64da\u67e5\u8a62\uff1f<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6574\u9ad4\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\uff08Stored Functions\uff09\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u512a\u5316\u6578\u64da\u67e5\u8a62\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\u4f86\u512a\u5316\u6578\u64da\u67e5\u8a62\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u5efa\u8b70\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e5%ba%ab%e8%aa%bf%e7%94%a8%e5%87%bd%e6%95%b8%ef%bc%9f-CmpTlFoUAn\">\u4ec0\u9ebc\u662f\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\uff1f<\/h2>\n<p>\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\u662f\u4e00\u7a2e\u5728\u6578\u64da\u5eab\u4e2d\u9810\u5148\u5b9a\u7fa9\u7684\u7a0b\u5e8f\uff0c\u5141\u8a31\u7528\u6236\u57f7\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\u6216\u8a08\u7b97\u3002\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u63a5\u6536\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u7d50\u679c\uff0c\u901a\u5e38\u7528\u65bc\u5c01\u88dd\u8907\u96dc\u7684\u67e5\u8a62\u908f\u8f2f\u6216\u8a08\u7b97\u3002\u4f7f\u7528\u8abf\u7528\u51fd\u6578\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u6e1b\u5c11\u7db2\u7d61\u6d41\u91cf\uff1a\u5c07\u908f\u8f2f\u79fb\u81f3\u6578\u64da\u5eab\u7aef\uff0c\u6e1b\u5c11\u5ba2\u6236\u7aef\u8207\u6578\u64da\u5eab\u4e4b\u9593\u7684\u4ea4\u4e92\u3002<\/li>\n<li>\u63d0\u9ad8\u6027\u80fd\uff1a\u5728\u6578\u64da\u5eab\u5167\u90e8\u57f7\u884c\u8a08\u7b97\uff0c\u901a\u5e38\u6bd4\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u57f7\u884c\u66f4\u5feb\u3002<\/li>\n<li>\u589e\u5f37\u5b89\u5168\u6027\uff1a\u901a\u904e\u9650\u5236\u76f4\u63a5\u8a2a\u554f\u6578\u64da\u8868\uff0c\u6e1b\u5c11\u6f5b\u5728\u7684\u5b89\u5168\u98a8\u96aa\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e8%aa%bf%e7%94%a8%e5%87%bd%e6%95%b8-CmpTlFoUAn\">\u5982\u4f55\u5275\u5efa\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578<\/h2>\n<p>\u5275\u5efa\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\u7684\u8a9e\u6cd5\u56e0\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff08DBMS\uff09\u800c\u7570\u3002\u4ee5\u4e0b\u662f MySQL \u4e2d\u5275\u5efa\u8abf\u7528\u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>\nCREATE FUNCTION function_name (parameters)\nRETURNS data_type\nBEGIN\n    -- \u51fd\u6578\u908f\u8f2f\n    RETURN value;\nEND;\n<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u9700\u8981\u8a08\u7b97\u67d0\u500b\u7522\u54c1\u7684\u6298\u6263\u50f9\u683c\uff0c\u53ef\u4ee5\u5275\u5efa\u5982\u4e0b\u7684\u8abf\u7528\u51fd\u6578\uff1a<\/p>\n<pre><code>\nCREATE FUNCTION calculate_discount(price DECIMAL(10,2), discount_rate DECIMAL(5,2))\nRETURNS DECIMAL(10,2)\nBEGIN\n    RETURN price - (price * discount_rate \/ 100);\nEND;\n<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e8%aa%bf%e7%94%a8%e5%87%bd%e6%95%b8%e5%84%aa%e5%8c%96%e6%9f%a5%e8%a9%a2-CmpTlFoUAn\">\u4f7f\u7528\u8abf\u7528\u51fd\u6578\u512a\u5316\u67e5\u8a62<\/h2>\n<p>\u4f7f\u7528\u8abf\u7528\u51fd\u6578\u53ef\u4ee5\u986f\u8457\u512a\u5316\u67e5\u8a62\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u67e5\u8a62\u4e2d\u4f7f\u7528\u6211\u5011\u525b\u624d\u5275\u5efa\u7684\u51fd\u6578\uff1a<\/p>\n<pre><code>\nSELECT product_id, product_name, calculate_discount(price, discount_rate) AS discounted_price\nFROM products;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u67e5\u8a62\u4e2d\uff0c\u6211\u5011\u76f4\u63a5\u5728 SQL \u8a9e\u53e5\u4e2d\u8abf\u7528 `calculate_discount` \u51fd\u6578\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u5728\u61c9\u7528\u5c64\u9032\u884c\u591a\u6b21\u8a08\u7b97\uff0c\u5f9e\u800c\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002<\/p>\n<h2 id=\"%e8%aa%bf%e7%94%a8%e5%87%bd%e6%95%b8%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-CmpTlFoUAn\">\u8abf\u7528\u51fd\u6578\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\u6642\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u6027\u80fd\u548c\u53ef\u7dad\u8b77\u6027\uff1a<\/p>\n<ul>\n<li><strong>\u907f\u514d\u904e\u5ea6\u4f7f\u7528\uff1a<\/strong>\u96d6\u7136\u8abf\u7528\u51fd\u6578\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u4f46\u904e\u5ea6\u4f7f\u7528\u53ef\u80fd\u6703\u5c0e\u81f4\u67e5\u8a62\u8b8a\u5f97\u8907\u96dc\uff0c\u5f71\u97ff\u53ef\u8b80\u6027\u3002<\/li>\n<li><strong>\u6e2c\u8a66\u6027\u80fd\uff1a<\/strong>\u5728\u751f\u7522\u74b0\u5883\u4e2d\u4f7f\u7528\u4e4b\u524d\uff0c\u61c9\u8a72\u5c0d\u8abf\u7528\u51fd\u6578\u9032\u884c\u6027\u80fd\u6e2c\u8a66\uff0c\u4ee5\u78ba\u4fdd\u5176\u4e0d\u6703\u6210\u70ba\u74f6\u9838\u3002<\/li>\n<li><strong>\u4fdd\u6301\u7c21\u55ae\uff1a<\/strong>\u51fd\u6578\u61c9\u8a72\u4fdd\u6301\u7c21\u55ae\uff0c\u5c08\u6ce8\u65bc\u55ae\u4e00\u4efb\u52d9\uff0c\u9019\u6a23\u66f4\u5bb9\u6613\u7dad\u8b77\u548c\u8abf\u8a66\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-CmpTlFoUAn\">\u7d50\u8ad6<\/h2>\n<p>\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u512a\u5316\u6578\u64da\u67e5\u8a62\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u901a\u904e\u5c07\u8a08\u7b97\u908f\u8f2f\u79fb\u81f3\u6578\u64da\u5eab\u7aef\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6e1b\u5c11\u7db2\u7d61\u6d41\u91cf\uff0c\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002\u9075\u5faa\u6700\u4f73\u5be6\u8e10\uff0c\u5408\u7406\u4f7f\u7528\u8abf\u7528\u51fd\u6578\uff0c\u5c07\u6709\u52a9\u65bc\u69cb\u5efa\u9ad8\u6548\u3001\u53ef\u7dad\u8b77\u7684\u6578\u64da\u5eab\u61c9\u7528\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528\u6578\u64da\u5eab\u8abf\u7528\u51fd\u6578\u4f86\u512a\u5316\u6578\u64da\u67e5\u8a62\uff0c\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\uff0c\u6e1b\u5c11\u91cd\u8907\u4ee3\u78bc\uff0c\u63d0\u5347\u6027\u80fd\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-175497","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175497","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=175497"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175497\/revisions"}],"predecessor-version":[{"id":175498,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175497\/revisions\/175498"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=175497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=175497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=175497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}