为什么这个查询总结来自不同表的不同值有效:
SELECT paymentMethodId, SUM(price) AS total FROM (
SELECT paymentMethodId, price FROM flights WHERE status >= 5
UNION ALL
SELECT paymentMethodId, price FROM cruises WHERE status >= 5
UNION ALL
SELECT paymentMethodId, price FROM cars WHERE status >= 5
UNION ALL
SELECT paymentMethodId, price FROM hotels WHERE status >= 5
) alias GROUP BY paymentMethodId
和这个(仅在WHERE条件下更改)不会:
SELECT paymentMethodId, SUM(price) AS total FROM (
SELECT paymentMethodId, price FROM flights WHERE status = 5
UNION ALL
SELECT paymentMethodId, price FROM cruises WHERE status = 5
UNION ALL
SELECT paymentMethodId, price FROM cars WHERE status = 5
UNION ALL
SELECT paymentMethodId, price FROM hotels WHERE status = 5
) alias GROUP BY paymentMethodId
我必须指出,只有在汽车桌上才有现有行。