{"id":5870,"date":"2024-11-14T13:26:53","date_gmt":"2024-11-14T05:26:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5870\/"},"modified":"2024-11-14T13:26:53","modified_gmt":"2024-11-14T05:26:53","slug":"sql%e4%b8%a4%e8%a1%a8%e5%85%b3%e8%81%94%e6%80%8e%e4%b9%88%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5870\/","title":{"rendered":"sql\u4e24\u8868\u5173\u8054\u600e\u4e48\u67e5"},"content":{"rendered":"<p><strong>sql \u4e24\u8868\u5173\u8054\u67e5\u8be2<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u5173\u8054\u662f\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8868\u4e2d\u7684\u6570\u636e\u8fde\u63a5\u8d77\u6765\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u68c0\u7d22\u8de8\u591a\u4e2a\u8868\u7684\u6570\u636e\uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5168\u9762\u7684\u89c6\u56fe\u3002<\/p>\n<p><strong>\u5982\u4f55\u8fdb\u884c\u4e24\u8868\u5173\u8054\u67e5\u8be2<\/strong><\/p>\n<p>\u6700\u5e38\u89c1\u7684\u5173\u8054\u7c7b\u578b\u662f INNER JOIN\uff0c\u5b83\u53ea\u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d\u5177\u6709\u5339\u914d\u884c\u7684\u8bb0\u5f55\uff1a<\/p>\n<pre>SELECT *\nFROM table1\nINNER JOIN table2\nON table1.id = table2.id;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u67e5\u8be2\u4e2d\uff1a<\/p>\n<ul>\n<li>table1 \u548c table2 \u662f\u8981\u5173\u8054\u7684\u8868\u3002<\/li>\n<li>ON table1.id = table2.id \u662f\u8fde\u63a5\u6761\u4ef6\uff0c\u5b83\u6307\u5b9a\u4e86\u4e24\u4e2a\u8868\u5e94\u8be5\u6839\u636e\u54ea\u4e00\u5217\u8fdb\u884c\u5173\u8054\u3002<\/li>\n<\/ul>\n<p><strong>\u5176\u4ed6\u7c7b\u578b\u7684\u5173\u8054<\/strong><\/p>\n<p>\u9664\u4e86 INNER JOIN \u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u7c7b\u578b\u7684\u5173\u8054\u7528\u4e8e\u4e0d\u540c\u7684\u76ee\u7684\uff1a<\/p>\n<ul>\n<li> <strong>LEFT JOIN\uff1a<\/strong>\u8fd4\u56de\u5de6\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002<\/li>\n<li> <strong>RIGHT JOIN\uff1a<\/strong>\u8fd4\u56de\u53f3\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002<\/li>\n<li> <strong>FULL JOIN\uff1a<\/strong>\u8fd4\u56de\u6765\u81ea\u4e24\u4e2a\u8868\u7684\u6240\u6709\u884c\uff0c\u65e0\u8bba\u662f\u5426\u5b58\u5728\u5339\u914d\u7684\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u5173\u8054\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a\u5ba2\u6237 \u548c \u8ba2\u5355\u3002\u5ba2\u6237\u8868\u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff0c\u800c\u8ba2\u5355\u8868\u5305\u542b\u8ba2\u5355\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5173\u8054\u67e5\u8be2\u6765\u67e5\u627e\u7279\u5b9a\u5ba2\u6237\u7684\u6240\u6709\u8ba2\u5355\uff1a<\/p>\n<pre>SELECT *\nFROM \u5ba2\u6237\nINNER JOIN \u8ba2\u5355\nON \u5ba2\u6237.id = \u8ba2\u5355.\u5ba2\u6237_id\nWHERE \u5ba2\u6237.\u59d3\u540d = '\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u67e5\u8be2\u5c06\u8fd4\u56de\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\u7684\u6240\u6709\u8ba2\u5355\u3002<\/p>\n<p><strong>\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5173\u8054\u65f6\uff0c\u786e\u4fdd\u8fde\u63a5\u6761\u4ef6\u662f\u552f\u4e00\u7684\uff0c\u4ee5\u907f\u514d\u8fd4\u56de\u91cd\u590d\u7684\u884c\u3002<\/li>\n<li>\u4f7f\u7528\u9002\u5f53\u7684\u5173\u8054\u7c7b\u578b\u4ee5\u83b7\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/li>\n<li>\u5728\u8fde\u63a5\u8868\u4e4b\u524d\u5bf9\u5b83\u4eec\u8fdb\u884c\u7d22\u5f15\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e24\u8868\u5173\u8054\u600e\u4e48\u67e5\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>sql \u4e24\u8868\u5173\u8054\u67e5\u8be2 \u5728 SQL \u4e2d\uff0c\u5173\u8054\u662f\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8868\u4e2d\u7684\u6570\u636e\u8fde\u63a5\u8d77\u6765\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u68c0\u7d22\u8de8\u591a\u4e2a\u8868\u7684\u6570\u636e\uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5168\u9762\u7684\u89c6\u56fe\u3002 \u5982\u4f55\u8fdb\u884c\u4e24\u8868\u5173\u8054\u67e5\u8be2 \u6700\u5e38\u89c1\u7684\u5173\u8054\u7c7b\u578b\u662f INNER JOIN\uff0c\u5b83\u53ea\u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d\u5177\u6709\u5339\u914d\u884c\u7684\u8bb0\u5f55\uff1a SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id; \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u67e5\u8be2\u4e2d\uff1a table1 \u548c table2 \u662f\u8981\u5173\u8054\u7684\u8868\u3002 ON table1.id = table2.id \u662f\u8fde\u63a5\u6761\u4ef6\uff0c\u5b83\u6307\u5b9a\u4e86\u4e24\u4e2a\u8868\u5e94\u8be5\u6839\u636e\u54ea\u4e00\u5217\u8fdb\u884c\u5173\u8054\u3002 \u5176\u4ed6\u7c7b\u578b\u7684\u5173\u8054 \u9664\u4e86 INNER JOIN \u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u7c7b\u578b\u7684\u5173\u8054\u7528\u4e8e\u4e0d\u540c\u7684\u76ee\u7684\uff1a LEFT JOIN\uff1a\u8fd4\u56de\u5de6\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002 RIGHT JOIN\uff1a\u8fd4\u56de\u53f3\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u884c\u3002 FULL JOIN\uff1a\u8fd4\u56de\u6765\u81ea\u4e24\u4e2a\u8868\u7684\u6240\u6709\u884c\uff0c\u65e0\u8bba\u662f\u5426\u5b58\u5728\u5339\u914d\u7684\u884c\u3002 \u5173\u8054\u793a\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a\u5ba2\u6237 \u548c \u8ba2\u5355\u3002\u5ba2\u6237\u8868\u5305\u542b\u5ba2\u6237\u4fe1\u606f\uff0c\u800c\u8ba2\u5355\u8868\u5305\u542b\u8ba2\u5355\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5173\u8054\u67e5\u8be2\u6765\u67e5\u627e\u7279\u5b9a\u5ba2\u6237\u7684\u6240\u6709\u8ba2\u5355\uff1a SELECT * FROM \u5ba2\u6237 INNER JOIN \u8ba2\u5355 ON \u5ba2\u6237.id [&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-5870","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5870","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=5870"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5870\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}