开发者

help with sql query - count

i have 3 tables i开发者_如何学Pythonn sql-server 2008

table A , table B , Table C

i need to count all 3 tables and see it in one query line, like this:

A  B   C
30 40 12

i tried this: select count(*) from A,select count(*) from B,select count(*) from C

but i got error

thank's in advance


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


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


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


The other solutions are a little cleaner, but... here's one more way. :)

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜