简体   繁体   English

MySQL:多个SELECT和OUTER JOIN

[英]MySQL : Multiple SELECT and OUTER JOIN

I'm quite new with MySQL, and having trouble arranging some tables, I can't seem to find a solution anywhere... 我是MySQL的新手,在安排一些表时遇到麻烦,我似乎在任何地方都找不到解决方案...

To simplify, say I have two types of tables, a and b, the first in 3 pieces and the second in 2 pieces. 为简单起见,假设我有两种类型的表,a和b,第一种是3件,第二种是2件。 So a1, a2, a3 that I want to merge with b1, b2 by the first column. 我想在第一列中将a1,a2,a3与b1,b2合并。

To join a1 and b1 I do : 要加入a1和b1,我要做的是:

SELECT a.column1, a.column2
FROM table_a1 AS a
LEFT OUTER JOIN table_b1 AS b ON (a.column1=b.column1)

Now how can I select from a1, a2, a3 and join with b1, b2 ? 现在如何从a1,a2,a3中选择并与b1,b2联接?

It seems like the design might need some work. 似乎设计可能需要一些工作。

But for now, you can do this: 但是现在,您可以执行以下操作:

SELECT 
  a.column1, a.column2
FROM 
  (SELECT * FROM table_a1
   UNION ALL SELECT * FROM table_a2
   UNION ALL SELECT * FROM table_a3) AS a 
LEFT OUTER JOIN 
  (SELECT * FROM table_b1
   UNION ALL SELECT * FROM table_b2) AS b
ON (a.column1 = b.column1)

It seems that you want something as simple as: 似乎您想要简单的东西:

SELECT * FROM table_a1 a
LEFT JOIN table_b1 b ON b.column1 = a.column1

But Golez there has a totally different answer, which makes me think I've read the question wrong =/ 但是Golez有一个完全不同的答案,这让我觉得我读错了这个问题= /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM