我试图通过一个 SQL 查询来获得 5 个不同表中结果的总和。我有 5 个表,每个表可能包含属于订单的记录。5 个表中所有记录的总和决定了总订单价格。
在这个论坛上搜索我想出了以下查询:
SELECT
OrderTotal.total + Shipping.amount + Service.amount - Gift.amount - RMA.total as TotalCosts
FROM
(SELECT SUM(price * amount) AS total FROM order WHERE order_id=123456) OrderTotal
(SELECT amount FROM shipping WHERE order_id=123456) Shipping,
(SELECT amount FROM service WHERE order_id=123456) Service,
(SELECT SUM(price * amount) AS total FROM rma WHERE order_id=123456) RMA,
(SELECT amount FROM gift WHERE order_id=123456) Gift
我现在面临的问题是,例如,当最后一个 SELECT 返回 0 行时,不返回总 TotalCosts 。
如何解决这个问题?