我的查询有以下结果集:
OrderId CustomerId ProducerId CustomerPayment ProducerPayment
1 1 3 10 5
1 1 4 10 5
1 2 3 10 5
1 2 4 10 5
我需要将此结果返回到此:
OrderId UserId Payment
1 1 20
1 2 20
1 3 10
1 4 10
只需将 CustomerId
和 ProducerId
合并为 UserId
。与付款
列相同。
有没有什么方法可以仅使用简单的select
和group by
来实现这一点?我避免使用临时表,而是调用多个相同的查询等进行优化。我希望这是可能的。
非常感谢
最佳答案
SELECT
OrderId,
CustomerID AS UserId,
SUM (CustomerPayment) As Payment
FROM orders
UNION ALL
SELECT
OrderId,
ProducerId AS UserId,
SUM (ProducerPayment) As Payment
FROM orders
关于MySql结果集合并列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255073/