[英]Mysql sum column from multiple table in the same id
我在mysql中有18個表。
所有表都有一列結果(例如a.result,b.result等。)我需要選擇18個表的結果,並對所有id求和。
d.id 1 = a.result + b.result + c.result
(所有表中的id為1)
謝謝
使用UNION ALL
從所有18個表中獲取所有值。 然后使用SUM
函數。
詢問
SELECT SUM(t.result) FROM(
SELECT result FROM table_1
UNION ALL
SELECT result FROM table_2
UNION ALL
...........................
...........................
SELECT result FROM table_18
)t;
如果要從表中獲取特定ID的result
列值。 然后,使用WHERE
。
它不是很漂亮,但是類似的東西會起作用
SELECT a.result + b.result + c.result -- (All the way to r.result...)
FROM TableA a
INNER JOIN TableB b
ON a.ID = b.ID
INNER JOIN TableC c
ON b.ID = c.ID
-- (All the way to TableR ...)
除非您絕對確定ID將始終存在於所有表中,否則您可能要考慮使用OUTER JOINS
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.