如何将计数查询与另一个具有所有计数的表连接?

时间:2016-11-04 04:12:16

标签: oracle join count

我选择了几个表的计数,我有一个名为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加入这两件事。如果我错过了一些东西,有人可以帮助我吗?

1 个答案:

答案 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