合并2个子查询

时间:2015-11-05 19:17:17

标签: mysql sql view

我有两个子查询,它们来自两个不同的表中的相同列,我想将这些表转到包含两个子查询中的行的视图。

(SELECT one AS a, two AS b FROM table_a WHERE condition_a=true) 
(SELECT tree AS a, four AS b FROM table_b WHERE condition_b=true) 

( For the example lets assume that all data is VARCHAR(10) ) 

从这两个查询我想用“a”和“b”列创建一个视图,其中包含两个查询的所有结果。

这可能吗?如果是我怎么能这样做?。

1 个答案:

答案 0 :(得分:2)

使用union all

create view v_ab as
    SELECT one AS a, two AS b FROM table_a WHERE condition_a=true
    union all
    SELECT tree AS a, four AS b FROM table_b WHERE condition_b=true;