我有两个这样的桌子
表格1
------------------------------------
date | name | charge | coupon|
------------------------------------
10/01 | Alex | 500 | 100 |
11/01 | Max | 1000 | 200 |
13/01 | | | |
表2
------------------------------------
date | name | error | refund |
------------------------------------
10/01 | Alex | crash | |
| | | |
如果table2中存在错误,我想计算从table1向table2的退款。考虑日期和名称是唯一的。
我试过这样的东西,但这根本不正确。我是SQL的初学者
SELECT date, name, charge - coupon as Refund IF( error=True FROM table2 )
FROM table1
请帮助如何解决这个问题...
嗯。。。我想你想要一个join
:
select t2.*, (t1.charge - t1.coupon) as refund
from table2 t2 join
table1 t1
on t1.name = t2.name and t1.date = t2.date
where t2.error is not null;
如果您想要所有行,则有条件地在其中进行退款:
select t2.*,
(case when t2.error is not null then t1.charge - t1.coupon end) as refund
from table2 t2 join
table1 t1
on t1.name = t2.name and t1.date = t2.date;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句