0
我目前正在試圖總結一個MySQL語句的結果,它給了我幾個不同的表的行數。SQL查詢SUM從SELECT COUNT語句與UNION的表格
我已經研究了關於這個問題的每一個stackoverflow的解決方案。不幸的是,我無法弄清楚我的案例的正確語法。
我的結果工作陳述不概括:
SET @shipmentId=456;
SELECT
(SELECT COUNT(*) FROM Table1 WHERE ShipmentID = @shipmentId)
UNION ALL
(SELECT COUNT(*) FROM Table2 WHERE ShipmentID = @shipmentId)
UNION ALL
(SELECT COUNT(*) FROM Table3 WHERE ShipmentID = @shipmentId)
UNION ALL
(SELECT COUNT(*)FROM Table4 WHERE ShipmentID = @shipmentId)
UNION ALL
(SELECT COUNT(*)FROM Table5 WHERE ShipmentID = @shipmentId);
在我看來,這應該是最近的一個正確的解決辦法,但我不知道(試過很多變種):
SET @shipmentId=456;
SELECT SUM(MeasuredValues) FROM
SELECT(
(SELECT COUNT(*) FROM Table1 WHERE ShipmentID = @shipmentId) AS MeasuredValues
UNION ALL
(SELECT COUNT(*) FROM Table2 WHERE ShipmentID = @shipmentId) AS MeasuredValues
UNION ALL
(SELECT COUNT(*) FROM Table3 WHERE ShipmentID = @shipmentId) AS MeasuredValues
UNION ALL
(SELECT COUNT(*)FROM Table4 WHERE ShipmentID = @shipmentId) AS MeasuredValues
UNION ALL
(SELECT COUNT(*)FROM Table5 WHERE ShipmentID = @shipmentId) AS MeasuredValues) t1;
在此先感謝您的幫助。
已經嘗試過以下鏈接: StackOverFlow Solution1
上解決了我的SQL語法戰鬥非常感謝! –