【问题标题】:How to join a count query with another table which has all counts?如何将计数查询与另一个具有所有计数的表连接起来?
【发布时间】:2017-03-17 20:16:00
【问题描述】:

我正在选择几个表的计数,并且我有一个名为 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 来连接这两个东西。如果我缺少一些东西,有人可以帮助我吗?

【问题讨论】:

    标签: oracle join count


    【解决方案1】:

    您正在搜索类似的内容?

    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
    

    【讨论】:

    • 谢谢..这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多