{"id":193321,"date":"2024-11-12T21:11:23","date_gmt":"2024-11-12T13:11:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/193321\/"},"modified":"2024-11-12T21:11:24","modified_gmt":"2024-11-12T13:11:24","slug":"%e8%a8%b1%e5%a4%9asql%e6%80%a7%e8%83%bd%e5%95%8f%e9%a1%8c%e4%be%86%e8%87%aa%e6%96%bc%e4%b8%8d%e5%bf%85%e8%a6%81%e7%9a%84%e5%bc%b7%e5%88%b6%e6%80%a7%e5%b7%a5%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/193321\/","title":{"rendered":"\u8a31\u591aSQL\u6027\u80fd\u554f\u984c\u4f86\u81ea\u65bc\u201c\u4e0d\u5fc5\u8981\u7684\u5f37\u5236\u6027\u5de5\u4f5c\u201d"},"content":{"rendered":"<h1 id=\"%e8%a8%b1%e5%a4%9asql%e6%80%a7%e8%83%bd%e5%95%8f%e9%a1%8c%e4%be%86%e8%87%aa%e6%96%bc%e4%b8%8d%e5%bf%85%e8%a6%81%e7%9a%84%e5%bc%b7%e5%88%b6%e6%80%a7%e5%b7%a5%e4%bd%9c-gErTAgCDah\">\u8a31\u591aSQL\u6027\u80fd\u554f\u984c\u4f86\u81ea\u65bc\u201c\u4e0d\u5fc5\u8981\u7684\u5f37\u5236\u6027\u5de5\u4f5c\u201d<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0cSQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u7684\u4e3b\u8981\u5de5\u5177\u4e4b\u4e00\u3002\u7136\u800c\uff0c\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u5eab\u7ba1\u7406\u54e1\u5728\u4f7f\u7528SQL\u6642\uff0c\u5e38\u5e38\u6703\u9047\u5230\u6027\u80fd\u554f\u984c\u3002\u9019\u4e9b\u554f\u984c\u7684\u6839\u6e90\u5f80\u5f80\u4f86\u81ea\u65bc\u201c\u4e0d\u5fc5\u8981\u7684\u5f37\u5236\u6027\u5de5\u4f5c\u201d\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u9019\u4e00\u73fe\u8c61\u7684\u539f\u56e0\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e4%b8%8d%e5%bf%85%e8%a6%81%e7%9a%84%e5%bc%b7%e5%88%b6%e6%80%a7%e5%b7%a5%e4%bd%9c-gErTAgCDah\">\u4ec0\u9ebc\u662f\u201c\u4e0d\u5fc5\u8981\u7684\u5f37\u5236\u6027\u5de5\u4f5c\u201d<\/h2>\n<p>\u5728SQL\u4e2d\uff0c\u201c\u4e0d\u5fc5\u8981\u7684\u5f37\u5236\u6027\u5de5\u4f5c\u201d\u6307\u7684\u662f\u90a3\u4e9b\u5728\u67e5\u8a62\u57f7\u884c\u904e\u7a0b\u4e2d\uff0c\u7121\u6cd5\u6709\u6548\u5229\u7528\u8cc7\u6e90\u6216\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u7684\u64cd\u4f5c\u3002\u9019\u4e9b\u64cd\u4f5c\u53ef\u80fd\u5305\u62ec\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u6aa2\u7d22\u3001\u91cd\u8907\u8a08\u7b97\u3001\u904e\u5ea6\u7684\u806f\u63a5\u7b49\u3002\u9019\u4e9b\u554f\u984c\u4e0d\u50c5\u5f71\u97ff\u67e5\u8a62\u7684\u57f7\u884c\u901f\u5ea6\uff0c\u9084\u53ef\u80fd\u5c0e\u81f4\u7cfb\u7d71\u8cc7\u6e90\u7684\u6d6a\u8cbb\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e7%9a%84sql%e6%80%a7%e8%83%bd%e5%95%8f%e9%a1%8c-gErTAgCDah\">\u5e38\u898b\u7684SQL\u6027\u80fd\u554f\u984c<\/h2>\n<h3 id=\"1-%e4%b8%8d%e5%bf%85%e8%a6%81%e7%9a%84%e6%95%b8%e6%93%9a%e6%aa%a2%e7%b4%a2-gErTAgCDah\">1. \u4e0d\u5fc5\u8981\u7684\u6578\u64da\u6aa2\u7d22<\/h3>\n<p>\u8a31\u591a\u958b\u767c\u8005\u5728\u64b0\u5bebSQL\u67e5\u8a62\u6642\uff0c\u5f80\u5f80\u6703\u9078\u64c7\u4f7f\u7528\u201cSELECT *\u201d\u4f86\u6aa2\u7d22\u6240\u6709\u5217\u3002\u9019\u6a23\u7684\u505a\u6cd5\u96d6\u7136\u65b9\u4fbf\uff0c\u4f46\u537b\u6703\u5c0e\u81f4\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u50b3\u8f38\u548c\u8655\u7406\u3002\u76f8\u53cd\uff0c\u61c9\u8a72\u660e\u78ba\u6307\u5b9a\u6240\u9700\u7684\u5217\uff0c\u4ee5\u6e1b\u5c11\u6578\u64da\u91cf\u3002<\/p>\n<pre><code>-- \u4e0d\u63a8\u85a6\u7684\u5beb\u6cd5\nSELECT * FROM users;\n\n-- \u63a8\u85a6\u7684\u5beb\u6cd5\nSELECT id, name, email FROM users;<\/code><\/pre>\n<h3 id=\"2-%e9%87%8d%e8%a4%87%e8%a8%88%e7%ae%97-gErTAgCDah\">2. \u91cd\u8907\u8a08\u7b97<\/h3>\n<p>\u5728\u67e5\u8a62\u4e2d\uff0c\u5982\u679c\u591a\u6b21\u4f7f\u7528\u76f8\u540c\u7684\u8a08\u7b97\uff0c\u5247\u6703\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u3002\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u5b50\u67e5\u8a62\u6216CTE\uff08\u516c\u7528\u8868\u8868\u9054\u5f0f\uff09\u4f86\u907f\u514d\u91cd\u8907\u8a08\u7b97\u3002<\/p>\n<pre><code>-- \u4e0d\u63a8\u85a6\u7684\u5beb\u6cd5\nSELECT id, (price * quantity) AS total FROM orders;\nSELECT id, (price * quantity) AS total FROM order_details;\n\n-- \u63a8\u85a6\u7684\u5beb\u6cd5\nWITH OrderTotals AS (\n    SELECT id, (price * quantity) AS total FROM orders\n)\nSELECT * FROM OrderTotals;<\/code><\/pre>\n<h3 id=\"3-%e9%81%8e%e5%ba%a6%e7%9a%84%e8%81%af%e6%8e%a5-gErTAgCDah\">3. \u904e\u5ea6\u7684\u806f\u63a5<\/h3>\n<p>\u5728\u8655\u7406\u591a\u500b\u8868\u4e4b\u9593\u7684\u6578\u64da\u6642\uff0c\u904e\u5ea6\u7684\u806f\u63a5\u6703\u5c0e\u81f4\u67e5\u8a62\u6027\u80fd\u4e0b\u964d\u3002\u61c9\u8a72\u4ed4\u7d30\u8003\u616e\u806f\u63a5\u7684\u5fc5\u8981\u6027\uff0c\u4e26\u76e1\u91cf\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u806f\u63a5\u64cd\u4f5c\u3002<\/p>\n<pre><code>-- \u4e0d\u63a8\u85a6\u7684\u5beb\u6cd5\nSELECT a.*, b.* FROM table_a a JOIN table_b b ON a.id = b.a_id JOIN table_c c ON b.id = c.b_id;\n\n-- \u63a8\u85a6\u7684\u5beb\u6cd5\nSELECT a.id, a.name, b.value FROM table_a a JOIN table_b b ON a.id = b.a_id;<\/code><\/pre>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%84%aa%e5%8c%96sql%e6%9f%a5%e8%a9%a2-gErTAgCDah\">\u5982\u4f55\u512a\u5316SQL\u67e5\u8a62<\/h2>\n<p>\u70ba\u4e86\u6e1b\u5c11\u201c\u4e0d\u5fc5\u8981\u7684\u5f37\u5236\u6027\u5de5\u4f5c\u201d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u5e7e\u500b\u512a\u5316\u63aa\u65bd\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u7d22\u5f15\uff1a<\/strong>\u9069\u7576\u5730\u70ba\u8868\u4e2d\u7684\u5217\u5275\u5efa\u7d22\u5f15\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002<\/li>\n<li><strong>\u5206\u6790\u67e5\u8a62\u8a08\u5283\uff1a<\/strong>\u4f7f\u7528\u6578\u64da\u5eab\u63d0\u4f9b\u7684\u5de5\u5177\u4f86\u5206\u6790\u67e5\u8a62\u8a08\u5283\uff0c\u627e\u51fa\u6027\u80fd\u74f6\u9838\u3002<\/li>\n<li><strong>\u5b9a\u671f\u6e05\u7406\u6578\u64da\uff1a<\/strong>\u522a\u9664\u4e0d\u5fc5\u8981\u7684\u6578\u64da\uff0c\u4fdd\u6301\u6578\u64da\u5eab\u7684\u6574\u6f54\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7de9\u5b58\uff1a<\/strong>\u5c0d\u65bc\u983b\u7e41\u67e5\u8a62\u7684\u6578\u64da\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u7de9\u5b58\u6280\u8853\u4f86\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-gErTAgCDah\">\u7d50\u8ad6<\/h2>\n<p>\u5728SQL\u67e5\u8a62\u4e2d\uff0c\u8a31\u591a\u6027\u80fd\u554f\u984c\u6e90\u65bc\u201c\u4e0d\u5fc5\u8981\u7684\u5f37\u5236\u6027\u5de5\u4f5c\u201d\u3002\u901a\u904e\u512a\u5316\u67e5\u8a62\u3001\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u6aa2\u7d22\u548c\u8a08\u7b97\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u6027\u80fd\u3002\u4e86\u89e3\u9019\u4e9b\u554f\u984c\u4e26\u63a1\u53d6\u76f8\u61c9\u7684\u63aa\u65bd\uff0c\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u6574\u9ad4\u7cfb\u7d71\u7684\u6548\u7387\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\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u512a\u5316\u6578\u64da\u5eab\u6027\u80fd\uff0c\u63d0\u5347\u696d\u52d9\u904b\u884c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u89e3\u6c7aSQL\u6027\u80fd\u554f\u984c\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5f37\u5236\u6027\u5de5\u4f5c\uff0c\u63d0\u5347\u67e5\u8a62\u6548\u7387\u8207\u7cfb\u7d71\u6548\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-193321","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193321","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=193321"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193321\/revisions"}],"predecessor-version":[{"id":193322,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193321\/revisions\/193322"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=193321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=193321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=193321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}