我有沒有關係的數據。只需要計算3個表格中的各列,並將它們作爲視圖顯示在頁面上。SQL在一個查詢中有多個選擇計數語句
這是到目前爲止的代碼,但不工作:
SELECT COUNT(cars) AS A,
(SELECT COUNT(boats) FROM tableBoats) AS B,
(SELECT COUNT(trees) FROM tableTrees) AS C,
FROM tableCars
我有沒有關係的數據。只需要計算3個表格中的各列,並將它們作爲視圖顯示在頁面上。SQL在一個查詢中有多個選擇計數語句
這是到目前爲止的代碼,但不工作:
SELECT COUNT(cars) AS A,
(SELECT COUNT(boats) FROM tableBoats) AS B,
(SELECT COUNT(trees) FROM tableTrees) AS C,
FROM tableCars
SELECT A, B, C
FROM (SELECT COUNT(cars) as A FROM tableCars) a
CROSS JOIN (SELECT COUNT(boats) as B FROM tableBoats) b
CROSS JOIN (SELECT COUNT(trees) as C FROM tableTrees) c
應該這樣做。
這工作!謝謝。我試圖接受答案,但它告訴我等5分鐘。生病後就做吧! – user999690
@ user999690您不必快速接受答案。當一個問題還沒有得到答案時,它動機人們回答它。那麼你可以有多個解決方案來解決你的問題:-) –
SELECT *
FROM
(
SELECT 'Nb cars' as description, COUNT(cars) AS count_item FROM tableCars
UNION ALL
SELECT 'Nb boats' as description, COUNT(boats) AS count_item FROM tableBoats
UNION ALL
SELECT 'Nb tress' as description, COUNT(trees) AS count_item FROM tableTrees
) temp
假設你有喜歡這裏的表(有一個名爲xxx
場tableXxx
表),查詢由具有逗號有語法錯誤AS C,
後,沒有那個逗號,它工作正常(至少使用SQLite,因爲MSSQL是不是在sqlfiddle對我來說)工作:
http://sqlfiddle.com/#!5/5fa6c/3
SELECT COUNT(cars) AS A,
(SELECT COUNT(boats) FROM tableBoats) AS B,
(SELECT COUNT(trees) FROM tableTrees) AS C
FROM tableCars
順便說一句,你可以簡化您的查詢
SELECT (SELECT COUNT(cars) FROM tableCars) AS A,
(SELECT COUNT(boats) FROM tableBoats) AS B,
(SELECT COUNT(trees) FROM tableTrees) AS C
其他的答案也完美:)
由於我已經減少了2頁的加載時間超過200%,另一個減少了30% – user3455363
這個怎麼樣?
SELECT
(SELECT COUNT(*) FROM tableCars) car_count,
(SELECT COUNT(*) FROM tableBoats) boat_count,
(SELECT COUNT(*) FROM tableTrees) tree_count
繼呂克M的迴應,它提供了一個單一的列表,與具有單獨值的列。 你可以想象這是多麼有用可能是...
SELECT C.Accountnum as AccountNum, C.Address as Address, 'C' as Source
From CustTable C
Where C.AccountNum like '000%'
Union All
Select V.Accountnum as AccountNum, V.Name as Address, 'V' as Source
from VendTable V
Where V.AccountNum like 'A%'
什麼是什麼數據庫您使用的是你和錯誤? –