关于sql:从多个表中选择count(*)

Select count(*) from multiple tables

如何从两个不同的表(称为tab1tab2中选择count(*),结果:

1
2
Count_1   Count_2
123       456

我试过了:

1
SELECT COUNT(*) Count_1 FROM schema.tab1 UNION ALL SELECT COUNT(*) Count_2 FROM schema.tab2

但我只有:

1
2
3
Count_1
123
456

1
2
3
4
5
6
7
8
9
SELECT  (
        SELECT COUNT(*)
        FROM   tab1
        ) AS count1,
        (
        SELECT COUNT(*)
        FROM   tab2
        ) AS count2
FROM    dual


as same thing to accomplish Additional Information,SQL服务器,You need to remove the"从一双"part of the query。P></


只是因为它slightly不同:P></

1
2
3
4
5
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

它给transposed the answers(instead of one one table row,column)其他我不认为这是非常不同的。我认为明智的they should be等效性能。P></


我的经验是与SQL服务器,但你可以:P></

1
2
SELECT (SELECT COUNT(*) FROM table1) AS count1,
  (SELECT COUNT(*) FROM table2) AS count2

在SQL Server的get the result You are after。P></


方法:slightly other differentP></

1
2
3
4
5
6
7
8
9
10
11
12
13
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
/

当看不到任何其他的答案把这车。P></

如果你不喜欢我的钥匙和原子查询在each table:You can do thisP></

1
2
3
SELECT COUNT(DISTINCT tab1.id) AS count_t1,
       COUNT(DISTINCT tab2.id) AS count_t2
    FROM tab1, tab2

但我相信,quassnoi' WISE性能的解决方案是更好的,和一个会使用。P></


从我这里is to股P></

1选择从从相同的域不同的计数表P></

1
2
SELECT DISTINCT(SELECT COUNT(*) FROM domain1.table1)"count1", (SELECT COUNT(*) FROM domain1.table2)"count2"
FROM domain1.table1, domain1.table2;

选择2计数从不同的域相同的表P></

1
2
SELECT DISTINCT(SELECT COUNT(*) FROM domain1.table1)"count1", (SELECT COUNT(*) FROM domain2.table1)"count2"
FROM domain1.table1, domain2.table1;

option 3计数从不同的域的table with same to have"联盟的rows of countP></

1
2
3
4
5
SELECT 'domain 1'"domain", COUNT(*)
FROM domain1.table1
UNION ALL
SELECT 'domain 2', COUNT(*)
FROM domain2.table1;

共享SQL:茶,总是给你)P></


1
SELECT (SELECT COUNT(*) FROM tab1) count_1, (SELECT COUNT(*) FROM tab2) count_2 FROM dual;

SELECT (SELECT COUNT(*) FROM table1) + (SELECT COUNT(*) FROM table2) FROM dual;P></


来点查询完备性这将创建的查询给你的count of owner of the tables for a given。P></

1
2
3
4
5
6
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;

输出is something like theP></

1
2
3
4
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

然后,你可以运行你的counts to get。这只是一个方便的脚本to have在有时。P></


1
2
3
4
5
    SELECT
    t1.Count_1,t2.Count_2
    FROM
(SELECT COUNT(1) AS Count_1 FROM tab1) AS t1,
(SELECT COUNT(1) AS Count_2 FROM tab2) AS t2


在快速拨号:肉类与刺P></

1
SELECT (SELECT COUNT(*) FROM Table1) AS Count1, (SELECT COUNT(*) FROM Table2) AS Count2

说明:本测试的SQL服务器,我From Dualis not necessary(因此,the discrepancy)。P></


1
2
3
DECLARE @ALL INT
SET @ALL = (SELECT COUNT(*) FROM tab1) + (SELECT COUNT(*) FROM tab2)
Print @ALL

前P></

1
SELECT (SELECT COUNT(*) FROM tab1) + (SELECT COUNT(*) FROM tab2)

if the tables(least or a key column)are of the same type只是让联盟第一,然后计数。P></

1
2
3
4
5
SELECT COUNT(*)
  FROM (SELECT tab1key AS KEY FROM schema.tab1
        UNION ALL
        SELECT tab2key AS KEY FROM schema.tab2
       )

把你的satement or another SUM()把它周围。P></

1
2
3
4
SELECT SUM(amount) FROM
(
SELECT COUNT(*) amount FROM schema.tab1 UNION ALL SELECT COUNT(*) amount FROM schema.tab2
)

1
2
3
4
5
6
7
8
9
10
--============= 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

表join with differentP></

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


select(select count()值是从哪里tab1 field+类)(select count()值是从哪里fieldTAB2类)countP></


1
2
3
4
5
6
7
8
9
10
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