帮助sql查询 - 计数

时间:2010-11-05 16:37:39

标签: sql-server sql-server-2008

我在sql-server 2008中有3个表

table A , table B , Table C

我需要计算所有3个表并在一个查询行中查看它,如下所示:

A  B   C
30 40 12

我试过这个:select count(*) from A,select count(*) from B,select count(*) from C

但我收到了错误

提前谢谢

4 个答案:

答案 0 :(得分:4)

select
    (select count(*) from A) as A,
    (select count(*) from B) as B,
    (select count(*) from C) as C

答案 1 :(得分:2)

SELECT *
FROM 
(SELECT COUNT(*) AS A_Count
 FROM A) tmp,
(SELECT COUNT(*) AS B_Count
 FROM B) tmp2, 
(SELECT COUNT(*) AS C_Count
 FROM C) tmp3

答案 2 :(得分:1)

SELECT
    A = (SELECT COUNT(*) FROM A),
    B = (SELECT COUNT(*) FROM B),
    C = (SELECT COUNT(*) FROM C)

答案 3 :(得分:0)

其他解决方案有点清洁,但......这是另一种方法。 :)

SELECT SUM(CASE WHEN TableName = 'A' THEN RecordCount ELSE 0 END) AS A_Count,
SUM(CASE WHEN TableName = 'B' THEN RecordCount ELSE 0 END) AS B_Count,
SUM(CASE WHEN TableName = 'C' THEN RecordCount ELSE 0 END) AS C_Count
FROM
(
    SELECT 'A' AS TableName, COUNT(*) AS RecordCount FROM A
    UNION ALL
    SELECT 'B', COUNT(*) FROM B
    UNION ALL
    SELECT 'C', COUNT(*) FROM C
) q