我想将下面的查询合并到一个表中,但是'union'的结果是将它合并到一个列中。我希望它创建不同的列,而不是像图中所示的那样。
我如何实现这一目标?
我有这样的查询:
(select count(*) as col_1
from ref_A
inner join ref_B
on ref_A.id_A= ref_B.id_A
inner join ref_C
on ref_C.id_C= ref_B.id_C
group by ref_C.id_C)
union all
(select count(*) as col_2
from ref_B
inner join ref_C
on ref_C.id_C= ref_B.id_C
group by ref_C.id_C)
_________________
| col_1 | col_2 |
|_______|_______|
|1 |$ 2,00 |
-----------------
|2 |$ 3,50 |
-----------------
答案 0 :(得分:1)
试试这个,
(select count(*) as col_1, 'table 1' as ident
from ref_A
inner join ref_B
on ref_A.id_A= ref_B.id_A
inner join ref_C
on ref_C.id_C= ref_B.id_C
group by ref_C.id_C)
union all
(select count(*) as col_1, 'table 2' as ident
from ref_B
inner join ref_C
on ref_C.id_C= ref_B.id_C
group by ref_C.id_C)
这里ident
不会合并来自两个查询的数据,查询数据只有在找到相同的行时才会合并。