[英]Issue while using inner join with sum
I have two (2) tables: ps_wk_mp_seller_transaction_history 我有两(2)个表: ps_wk_mp_seller_transaction_history
And ps_wk_mp_seller_order_status 和ps_wk_mp_seller_order_status
I want to totalize seller_amount only if current_state = 5 仅在current_state = 5时,我才希望总计Seller_amount
For that I have written this: 为此,我写了这个:
select sum(seller_amount) as seller_amount
from ps_wk_mp_seller_transaction_history th inner join
ps_wk_mp_seller_order_status os
on os.id_order = th.id_transaction and
os.current_state = 5 and
th.id_customer_seller = 2;
For id_customer_seller=2 I get this 4984.020000 (4950+34.02) and it is exact 对于id_customer_seller = 2我得到了4984.020000(4950 + 34.02) ,这是准确的
But I get 25.848000 instead of NULL for id_customer_seller=5 但是对于id_customer_seller = 5我得到25.848000而不是NULL
Can someone help me? 有人能帮我吗?
May be you can test yourself, this the code: https://github.com/kulturman/fakerepo 也许您可以测试一下自己,这是代码: https : //github.com/kulturman/fakerepo
First of all your records have some logic issue, id_transaction
have tow transactions with same id_transaction
but different amount and different state
! 首先您的记录有一些逻辑问题, id_transaction
有两个具有相同id_transaction
但数量不同和state
不同的交易! so transaction with id 41
having state 5
and that why customer 5
will not be null because 41 is in state 5. 因此ID为41
交易的状态为5
,为什么客户5
不会为null,因为41处于状态5。
To fix this 要解决这个问题
transaction id must be unique id for every transaction in order to differentiate between transaction state and amount 每个交易的交易ID必须是唯一ID,以便区分交易状态和金额
the query should be like this 查询应该是这样的
select sum(seller_amount) as seller_amount
from ps_wk_mp_seller_transaction_history th
left join ps_wk_mp_seller_order_status os
on os.id_order=th.id_transaction
where
th.id_customer_seller = 2
and os.current_state=5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.