我选择了几个表的计数,我有一个名为table_counts的表,它有两列名为table_name和table_count。我想将查询结果与table_counts表的table_name列一起加入。请参阅下面的示例。
select 'Table 1' as table_name, count(*) as table_count_from table_1
union
select 'Table 2' as table_name, count(*) as table_count_from table_2
union
select 'Table 3' as table_name, count(*) as table_count_from table_3
------------------
++table_counts++++
------------------
table_name table count
Table 1 10
Table 2 20
Table 3 30
我必须使用table_name加入这两件事。如果我错过了一些东西,有人可以帮助我吗?
答案 0 :(得分:0)
SELECT *
FROM table_counts cnt
LEFT OUTER JOIN (select 'Table 1' as table_name, count(*) as table_count_from table_1
union
select 'Table 2' as table_name, count(*) as table_count_from table_2
union
select 'Table 3' as table_name, count(*) as table_count_from table_3
) subcnt
ON cnt.table_name = subcnt.table_name