{"id":5729,"date":"2024-11-14T08:14:14","date_gmt":"2024-11-14T00:14:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5729\/"},"modified":"2024-11-14T08:14:14","modified_gmt":"2024-11-14T00:14:14","slug":"sql%e4%b8%bb%e9%94%ae%e5%a4%96%e9%94%ae%e6%80%8e%e4%b9%88%e5%85%b3%e8%81%94","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5729\/","title":{"rendered":"sql\u4e3b\u952e\u5916\u952e\u600e\u4e48\u5173\u8054"},"content":{"rendered":"<blockquote><p>\n  \u5728 sql \u4e2d\uff0c\u4e3b\u952e\u548c\u5916\u952e\u7528\u4e8e\u5173\u8054\u8868\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002\u4e3b\u952e\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u7684\u884c\uff0c\u800c\u5916\u952e\u5f15\u7528\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u3002\u901a\u8fc7\u4f7f\u7528 join \u5b50\u53e5\u5e76\u6307\u5b9a\u5173\u8054\u6761\u4ef6\uff0c\u53ef\u4ee5\u8fde\u63a5\u8868\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5728\u793a\u4f8b\u4e2d\uff0ccustomers.customerid \u548c orders.customerid \u4e4b\u95f4\u5efa\u7acb\u5173\u8054\uff0c\u8fd4\u56de\u5339\u914d\u884c\u7684\u5217\uff0c\u5141\u8bb8\u8de8\u8868\u8bbf\u95ee\u76f8\u5173\u8bb0\u5f55\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053021331918562.jpg\" class=\"aligncenter\" title=\"sql\u4e3b\u952e\u5916\u952e\u600e\u4e48\u5173\u8054\u63d2\u56fe\" alt=\"sql\u4e3b\u952e\u5916\u952e\u600e\u4e48\u5173\u8054\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e3b\u952e\u548c\u5916\u952e\u5173\u8054<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u4e3b\u952e\u548c\u5916\u952e\u7528\u4e8e\u5efa\u7acb\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u4ee5\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u4e3b\u952e<\/strong><\/p>\n<ul>\n<li>\u4e3b\u952e\u662f\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\uff0c\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u3002<\/li>\n<li>\u4e3b\u952e\u503c\u4e0d\u80fd\u4e3a NULL\uff0c\u5e76\u4e14\u5b83\u80fd\u591f\u533a\u5206\u8868\u4e2d\u6bcf\u6761\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p><strong>\u5916\u952e<\/strong><\/p>\n<ul>\n<li>\u5916\u952e\u662f\u53e6\u4e00\u4e2a\u8868\u4e2d\u4e3b\u952e\u7684\u5f15\u7528\u3002<\/li>\n<li>\u5916\u952e\u5217\u786e\u4fdd\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e76\u9632\u6b62\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<p><strong>\u5173\u8054<\/strong><\/p>\n<p>\u8981\u5173\u8054\u4e24\u5f20\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u5916\u952e\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\u3002\u4ee5\u4e0b\u6b65\u9aa4\u6982\u8ff0\u4e86\u5982\u4f55\u5173\u8054\u8868\uff1a<\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u4e3b\u952e\u548c\u5916\u952e\uff1a<\/strong>\u8bc6\u522b\u8981\u5173\u8054\u7684\u4e24\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u548c\u5916\u952e\u5217\u3002<\/li>\n<li> <strong>\u4f7f\u7528 JOIN \u5b50\u53e5\uff1a<\/strong>\u4f7f\u7528 JOIN \u5b50\u53e5\u5c06\u8868\u8fde\u63a5\u8d77\u6765\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 INNER JOIN\u3001LEFT JOIN \u6216 RIGHT JOIN\u3002<\/li>\n<li> <strong>\u6307\u5b9a\u5173\u8054\u6761\u4ef6\uff1a<\/strong>\u5728 JOIN \u5b50\u53e5\u4e2d\uff0c\u4f7f\u7528 ON \u5173\u952e\u5b57\u6307\u5b9a\u8fde\u63a5\u8868\u7684\u4e3b\u952e\u548c\u5916\u952e\u4e4b\u95f4\u7684\u5173\u8054\u6761\u4ef6\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li><strong>Customers (CustomerID, CustomerName, Address)<\/strong><\/li>\n<li><strong>Orders (OrderID, CustomerID, OrderDate)<\/strong><\/li>\n<\/ul>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> <strong>CustomerID<\/strong> \u662f Customers \u8868\u7684\u4e3b\u952e\u3002<\/li>\n<li> <strong>CustomerID<\/strong> \u662f Orders \u8868\u7684\u5916\u952e\u3002<\/li>\n<\/ul>\n<p>\u8981\u5173\u8054 Customers \u548c Orders \u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b JOIN \u5b50\u53e5\uff1a<\/p>\n<pre>SELECT *\nFROM Customers\nINNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u96c6\uff0c\u5176\u4e2d\u5305\u542b\u4e24\u4e2a\u8868\u4e2d\u5339\u914d\u884c\u7684\u5217\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u5173\u8054\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee Orders \u8868\u4e2d CustomerID \u5217\u76f8\u5173\u7684\u6240\u6709\u5ba2\u6237\u8bb0\u5f55\u3002\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee Customers \u8868\u4e2d OrderID \u5217\u76f8\u5173\u7684\u6240\u6709\u8ba2\u5355\u8bb0\u5f55\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e3b\u952e\u5916\u952e\u600e\u4e48\u5173\u8054\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 sql \u4e2d\uff0c\u4e3b\u952e\u548c\u5916\u952e\u7528\u4e8e\u5173\u8054\u8868\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002\u4e3b\u952e\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u7684\u884c\uff0c\u800c\u5916\u952e\u5f15\u7528\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u3002\u901a\u8fc7\u4f7f\u7528 join \u5b50\u53e5\u5e76\u6307\u5b9a\u5173\u8054\u6761\u4ef6\uff0c\u53ef\u4ee5\u8fde\u63a5\u8868\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5728\u793a\u4f8b\u4e2d\uff0ccustomers.customerid \u548c orders.customerid \u4e4b\u95f4\u5efa\u7acb\u5173\u8054\uff0c\u8fd4\u56de\u5339\u914d\u884c\u7684\u5217\uff0c\u5141\u8bb8\u8de8\u8868\u8bbf\u95ee\u76f8\u5173\u8bb0\u5f55\u3002 SQL \u4e3b\u952e\u548c\u5916\u952e\u5173\u8054 \u5728 SQL \u4e2d\uff0c\u4e3b\u952e\u548c\u5916\u952e\u7528\u4e8e\u5efa\u7acb\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u4ee5\u7ef4\u62a4\u6570\u636e\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002 \u4e3b\u952e \u4e3b\u952e\u662f\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\uff0c\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u3002 \u4e3b\u952e\u503c\u4e0d\u80fd\u4e3a NULL\uff0c\u5e76\u4e14\u5b83\u80fd\u591f\u533a\u5206\u8868\u4e2d\u6bcf\u6761\u8bb0\u5f55\u3002 \u5916\u952e \u5916\u952e\u662f\u53e6\u4e00\u4e2a\u8868\u4e2d\u4e3b\u952e\u7684\u5f15\u7528\u3002 \u5916\u952e\u5217\u786e\u4fdd\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e76\u9632\u6b62\u6570\u636e\u4e0d\u4e00\u81f4\u3002 \u5173\u8054 \u8981\u5173\u8054\u4e24\u5f20\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u5916\u952e\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\u3002\u4ee5\u4e0b\u6b65\u9aa4\u6982\u8ff0\u4e86\u5982\u4f55\u5173\u8054\u8868\uff1a \u786e\u5b9a\u4e3b\u952e\u548c\u5916\u952e\uff1a\u8bc6\u522b\u8981\u5173\u8054\u7684\u4e24\u4e2a\u8868\u4e2d\u7684\u4e3b\u952e\u548c\u5916\u952e\u5217\u3002 \u4f7f\u7528 JOIN \u5b50\u53e5\uff1a\u4f7f\u7528 JOIN \u5b50\u53e5\u5c06\u8868\u8fde\u63a5\u8d77\u6765\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 INNER JOIN\u3001LEFT JOIN \u6216 RIGHT JOIN\u3002 \u6307\u5b9a\u5173\u8054\u6761\u4ef6\uff1a\u5728 JOIN \u5b50\u53e5\u4e2d\uff0c\u4f7f\u7528 ON \u5173\u952e\u5b57\u6307\u5b9a\u8fde\u63a5\u8868\u7684\u4e3b\u952e\u548c\u5916\u952e\u4e4b\u95f4\u7684\u5173\u8054\u6761\u4ef6\u3002 \u793a\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a Customers (CustomerID, CustomerName, Address) Orders (OrderID, CustomerID, OrderDate) \u5176\u4e2d\uff1a CustomerID \u662f Customers \u8868\u7684\u4e3b\u952e\u3002 CustomerID [&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-5729","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5729","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=5729"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5729\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}