从多个表中选择count(*)


229

如何count(*)从具有结果的两个不同表(分别称为tab1tab2)中进行选择:

Count_1   Count_2
123       456

我已经试过了:

select count(*) Count_1 from schema.tab1 union all select count(*) Count_2 from schema.tab2

但是我所拥有的是:

Count_1
123
456

Answers:


327
SELECT  (
        SELECT COUNT(*)
        FROM   tab1
        ) AS count1,
        (
        SELECT COUNT(*)
        FROM   tab2
        ) AS count2
FROM    dual

14
你为什么需要双重?那是什么意思?
Ray Lu

31
这是一张有一张记录的假桌子。在Oracle中,如果没有FROM,就无法拥有SELECT。
Quassnoi

3
dual是oracle db中所有帐户均可访问的表,可以将其用于常见需求,例如:“ SELECT sysdate FROM dual”
dincerm

5
没关系,Oracle不会评估COUNT(*)内的任何内容。
Quassnoi

4
@Stéphane:在PostgreSQL上尝试Oracle代码时会发生这种情况。输了FROM dual
Quassnoi

81

作为附加信息,要在SQL Server中完成相同的操作,您只需删除查询的“ FROM dual”部分。


1
我只是准备说:“但是,当我看到您的评论时,MS SQL怎么办?谢谢您的需求!
Andrew Neely 2012年

40

只是因为它略有不同:

SELECT 'table_1' AS table_name, COUNT(*) FROM table_1
UNION
SELECT 'table_2' AS table_name, COUNT(*) FROM table_2
UNION
SELECT 'table_3' AS table_name, COUNT(*) FROM table_3

它给出了换位的答案(每张表一行而不是一列),否则我认为这没有太大不同。我认为在性能方面,它们应该是等效的。


1
您最好将UNION ALL放在这里。
Quassnoi

通过三个单行查询添加“ ALL”有什么区别?当然,两种方法的结果必须相同。
Mike Woodhouse

1
没有所有组的UNION结果。如果有2分排在TABLE_1和TABLE_2,并在TABLE_3 3行,你会在你的结果集中获得两行,将无法从结果集告诉多少行不TABLE_2有:2或3
Quassnoi

4
是的,但是我选择了表名,这使得结果唯一。否则,您将是正确的,但是在没有上下文的情况下,几个数字会有什么价值?;-)
Mike Woodhouse

这也是对每个计数使用CTE(WITH SELECT)语句的好方法。
blue_chip

28

我的经验是使用SQL Server,但您可以这样做:

select (select count(*) from table1) as count1,
  (select count(*) from table2) as count2

在SQL Server中,我得到的结果是您追求的。


11

其他略有不同的方法:

with t1_count as (select count(*) c1 from t1),
     t2_count as (select count(*) c2 from t2)
select c1,
       c2
from   t1_count,
       t2_count
/

select c1,
       c2
from   (select count(*) c1 from t1) t1_count,
       (select count(*) c2 from t2) t2_count
/

7

我看不到任何其他答案。

如果您不喜欢子查询并且在每个表中都有主键,则可以执行以下操作:

select count(distinct tab1.id) as count_t1,
       count(distinct tab2.id) as count_t2
    from tab1, tab2

但是从性能角度来看,我相信Quassnoi的解决方案更好,而且我会使用。


7

SELECT (SELECT COUNT(*) FROM table1) + (SELECT COUNT(*) FROM table2) FROM dual;


7

这是我分享的

选项1-从不同表中的同一域计数

select distinct(select count(*) from domain1.table1) "count1", (select count(*) from domain1.table2) "count2" 
from domain1.table1, domain1.table2;

选项2-从同一表的不同域计数

select distinct(select count(*) from domain1.table1) "count1", (select count(*) from domain2.table1) "count2" 
from domain1.table1, domain2.table1;

选项3-使用“全部合并”从同一表的不同域进行计数以具有计数行

select 'domain 1'"domain", count(*) 
from domain1.table1 
union all 
select 'domain 2', count(*) 
from domain2.table1;

享受SQL,我总是做:)




6

很快就想到了:

Select (select count(*) from Table1) as Count1, (select count(*) from Table2) as Count2

注意:我在SQL Server中对此进行了测试,因此From Dual没有必要(因此存在差异)。


5

出于完整性考虑,该查询将创建一个查询,以向您提供给定所有者的所有表的计数。

select 
  DECODE(rownum, 1, '', ' UNION ALL ') || 
  'SELECT ''' || table_name || ''' AS TABLE_NAME, COUNT(*) ' ||
  ' FROM ' || table_name  as query_string 
 from all_tables 
where owner = :owner;

输出是这样的

SELECT 'TAB1' AS TABLE_NAME, COUNT(*) FROM TAB1
 UNION ALL SELECT 'TAB2' AS TABLE_NAME, COUNT(*) FROM TAB2
 UNION ALL SELECT 'TAB3' AS TABLE_NAME, COUNT(*) FROM TAB3
 UNION ALL SELECT 'TAB4' AS TABLE_NAME, COUNT(*) FROM TAB4

然后可以运行以获取计数。有时只是一个方便的脚本。


4

如果表(或至少一个键列)的类型相同,则首先进行并集,然后进行计数。

select count(*) 
  from (select tab1key as key from schema.tab1 
        union all 
        select tab2key as key from schema.tab2
       )

或坐下,再加上一个sum()。

select sum(amount) from
(
select count(*) amount from schema.tab1 union all select count(*) amount from schema.tab2
)

3
--============= FIRST WAY (Shows as Multiple Row) ===============
SELECT 'tblProducts' [TableName], COUNT(P.Id) [RowCount] FROM tblProducts P
UNION ALL
SELECT 'tblProductSales' [TableName], COUNT(S.Id) [RowCount] FROM tblProductSales S


--============== SECOND WAY (Shows in a Single Row) =============
SELECT  
(SELECT COUNT(Id) FROM   tblProducts) AS ProductCount,
(SELECT COUNT(Id) FROM   tblProductSales) AS SalesCount

2
Declare @all int
SET @all = (select COUNT(*) from tab1) + (select count(*) from tab2)
Print @all

要么

SELECT (select COUNT(*) from tab1) + (select count(*) from tab2)

0

加入不同的表

SELECT COUNT(*) FROM (  
SELECT DISTINCT table_a.ID  FROM table_a JOIN table_c ON table_a.ID  = table_c.ID   );

0

选择(从tab1中field选择count(),如“值”)+(从tab2中选择count(),field如“值”)


-2
select @count = sum(data) from
(
select count(*)  as data from #tempregion
union 
select count(*)  as data from #tempmetro
union
select count(*)  as data from #tempcity
union
select count(*)  as data from #tempzips
) a

欢迎使用StackOverflow,并感谢您的张贴。请看看如何回答
Serge Belov 2012年

这个答案是错误的。无法使用联合(应全部使用联合)。
Deadsheep39
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.