Mysql 实现表关联查询更新

有如下需求:
根据B表的b字段更新A表的a字段,关联条件B.id=A.id。
当B表的b字段>0,a字段更新为1,否则更新为-1。
语句如下:

update A t 
inner join (select id, case when b>=0 then 1 else -1 end b from B)tt
set t.a = tt.b
where t.id = tt.id

猜你喜欢

转载自blog.csdn.net/qq_36743482/article/details/80019910