请教一条s'q'l 问题,感谢感谢

我想根据接收到的recordType来执行不同的sql,
看了一下应该时用case when吧,然后捣鼓了一下不行,
请求大神指点一二。

CASE when ats_wallet_record.record_type=1 THEN UPDATE ats_wallet set ats_wallet.balance =ats_wallet.balance+10 
when ats_wallet_record.record_type=2 THEN UPDATE ats_wallet set ats_wallet.balance =ats_wallet.balance-10
ELSE end 
FROM ats_wallet UNION ats_wallet_record ON ats_wallet.id=ats_wallet_record.wallet_id where ats_wallet.account_id=26421576974992178;

就是当recordType未1则钱包增加,2则减少。
谢谢

上面的判断逻辑最好用java/python这样的应用代码里实现最好

用数据库存储过程判断也可以

但是如果你一定要只用sql,有一种效率不太高的方法:
原理:将所有可能值拼接成子查询,外层套update, 参数传入子查询中进行过滤后去更新值

update ats_wallet as a set a.balance =b.balance from ( 

select 1 as recordType ,account_id, table.balance+10 as balance from table
union
select 2 as recordType ,account_id, table.balance-10 as balance from table) as b

where a.recordType=b.recordType and a.account_id=b.accout_id


写存储过程再用java去调

UPDATE ats_wallet ,ats_wallet_record set ats_wallet.balance = ats_wallet.balance +(case
ats_wallet_record.record_type
when 1 then 10
when 2 then -10
end
)
where ats_wallet.id=ats_wallet_record.wallet_id where ats_wallet.account_id=26421576974992178;
考虑到实际是为了更新,改成这种吧,mysql更新、删除都可以关联之后更新删除。亲测可以。