{"id":6182,"date":"2024-11-14T08:38:55","date_gmt":"2024-11-14T00:38:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6182\/"},"modified":"2024-11-14T08:38:55","modified_gmt":"2024-11-14T00:38:55","slug":"mysql%e5%88%86%e8%a1%a8%e5%90%8e%e5%a6%82%e4%bd%95%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6182\/","title":{"rendered":"mysql\u5206\u8868\u540e\u5982\u4f55\u67e5\u8be2"},"content":{"rendered":"<p><strong> \u5206\u8868\u540e\u67e5\u8be2\u65b9\u6cd5<\/strong><\/p>\n<p>\u5206\u8868\u662f\u5c06\u4e00\u5f20\u5927\u8868\u62c6\u5206\u6210\u591a\u4e2a\u5c0f\u8868\u7684\u4e00\u79cd\u6280\u672f\uff0c\u4ee5\u63d0\u5347\u6570\u636e\u5e93\u7684\u6027\u80fd\u548c\u53ef\u4f38\u7f29\u6027\u3002<\/p>\n<p><strong>\u67e5\u8be2\u5206\u8868\u6570\u636e\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528\u8054\u5408\u67e5\u8be2\uff08UNION\uff09<\/strong><\/p>\n<p>\u5c06\u6bcf\u4e2a\u5206\u8868\u7684\u7ed3\u679c\u96c6\u901a\u8fc7 UNION \u64cd\u4f5c\u7b26\u8054\u5408\u8d77\u6765\uff0c\u5f62\u6210\u4e00\u4e2a\u865a\u62df\u7684\u8868\u3002<\/p>\n<pre>SELECT * FROM table1\nUNION\nSELECT * FROM table2\nUNION\nSELECT * FROM table3;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 UNION ALL<\/strong><\/p>\n<p>\u4e0e UNION \u7c7b\u4f3c\uff0c\u4f46\u4e0d\u4f1a\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u53bb\u91cd\u3002<\/p>\n<pre>SELECT * FROM table1\nUNION ALL\nSELECT * FROM table2\nUNION ALL\nSELECT * FROM table3;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u5b50\u67e5\u8be2<\/strong><\/p>\n<p>\u4f7f\u7528\u5b50\u67e5\u8be2\u4ece\u6bcf\u4e2a\u5206\u8868\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u7136\u540e\u5c06\u5b50\u67e5\u8be2\u7ed3\u679c\u8054\u5408\u8d77\u6765\u3002<\/p>\n<pre>SELECT *\nFROM (\n  SELECT * FROM table1\n) AS t1\nUNION\nSELECT *\nFROM (\n  SELECT * FROM table2\n) AS t2\nUNION\nSELECT *\nFROM (\n  SELECT * FROM table3\n) AS t3;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u89c6\u56fe<\/strong><\/p>\n<p>\u521b\u5efa\u89c6\u56fe\u5c06\u5206\u8868\u7684\u6570\u636e\u865a\u62df\u6210\u4e00\u5f20\u8868\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u50cf\u67e5\u8be2\u5355\u8868\u4e00\u6837\u67e5\u8be2\u5206\u8868\u6570\u636e\u3002<\/p>\n<pre>CREATE VIEW my_view AS\nSELECT * FROM table1\nUNION\nSELECT * FROM table2\nUNION\nSELECT * FROM table3;\n\nSELECT * FROM my_view;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u67e5\u8be2\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5206\u8868\u7b56\u7565\u3001\u6570\u636e\u91cf\u548c\u67e5\u8be2\u8981\u6c42\u3002UNION \u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5927\u4e14\u9700\u8981\u53bb\u91cd\u7684\u573a\u666f\uff0c\u800c UNION ALL \u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u4e14\u4e0d\u9700\u8981\u53bb\u91cd\u7684\u573a\u666f\u3002\u5b50\u67e5\u8be2\u548c\u89c6\u56fe\u5219\u66f4\u52a0\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5b9a\u5236\u67e5\u8be2\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5206\u8868\u540e\u5982\u4f55\u67e5\u8be2\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>\u5206\u8868\u540e\u67e5\u8be2\u65b9\u6cd5 \u5206\u8868\u662f\u5c06\u4e00\u5f20\u5927\u8868\u62c6\u5206\u6210\u591a\u4e2a\u5c0f\u8868\u7684\u4e00\u79cd\u6280\u672f\uff0c\u4ee5\u63d0\u5347\u6570\u636e\u5e93\u7684\u6027\u80fd\u548c\u53ef\u4f38\u7f29\u6027\u3002 \u67e5\u8be2\u5206\u8868\u6570\u636e\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a 1. \u4f7f\u7528\u8054\u5408\u67e5\u8be2\uff08UNION\uff09 \u5c06\u6bcf\u4e2a\u5206\u8868\u7684\u7ed3\u679c\u96c6\u901a\u8fc7 UNION \u64cd\u4f5c\u7b26\u8054\u5408\u8d77\u6765\uff0c\u5f62\u6210\u4e00\u4e2a\u865a\u62df\u7684\u8868\u3002 SELECT * FROM table1 UNION SELECT * FROM table2 UNION SELECT * FROM table3; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 UNION ALL \u4e0e UNION \u7c7b\u4f3c\uff0c\u4f46\u4e0d\u4f1a\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u53bb\u91cd\u3002 SELECT * FROM table1 UNION ALL SELECT * FROM table2 UNION ALL SELECT * FROM table3; \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528\u5b50\u67e5\u8be2 \u4f7f\u7528\u5b50\u67e5\u8be2\u4ece\u6bcf\u4e2a\u5206\u8868\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u7136\u540e\u5c06\u5b50\u67e5\u8be2\u7ed3\u679c\u8054\u5408\u8d77\u6765\u3002 SELECT * FROM ( SELECT [&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-6182","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6182","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=6182"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6182\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}