我在SQL服務器3臺2008幫助的SQL查詢 - 算
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
但我
感謝的提前
0遇到錯誤我在SQL服務器3臺2008幫助的SQL查詢 - 算
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
但我
感謝的提前
0遇到錯誤select
(select count(*) from A) as A,
(select count(*) from B) as B,
(select count(*) from C) as C
SELECT
A = (SELECT COUNT(*) FROM A),
B = (SELECT COUNT(*) FROM B),
C = (SELECT COUNT(*) FROM 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 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
佈局比其他人更清晰易讀。 – 2010-11-05 16:49:41