在MySQL中运行两个INNER JOIN

时间:2015-04-30 15:08:57

标签: mysql inner-join

是否可以对两个单独的INNER JOIN进行OR运算,以便结果集包含来自两个INNER JOIN的数据?例如,MySQL中有以下几种可能。

SELECT * FROM table1
(INNER JOIN table2 ON table1.column_name=table2.column_name)
OR
(INNER JOIN table3 ON table1.column_name=table3.column_name)

3 个答案:

答案 0 :(得分:1)

不,你不能这样做,一种方法是使用union

SELECT * FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name
union
SELECT * FROM table1 INNER JOIN table3 ON table1.column_name=table3.column_name

答案 1 :(得分:0)

您可以使用UNION

SELECT * FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name
UNION DISTINCT
SELECT * FROM table1 INNER JOIN table3 ON table1.column_name=table3.column_name

正如at https://dev.mysql.com/doc/refman/4.1/en/union.html所述,UNION结合了多个选择的结果。

答案 2 :(得分:0)

您需要使用where

 select * from table1 
where

  (table1.column_name = (select table2.column_name from table2 inner join table1.column_name as  on table1.column_name = table2.column_name)) 

OR

  (table1.column_name = (select table3.column_name from table3   inner join table1.column_name as  on table1.column_name = table3.column_name)) 

在这里工作完美!