下面有4列这样的表:
product quantity value id
---------------------------
p1 -100 5 id1
p2 40 3 id1
p2 20 2 id1
p2 40 4 id1
我需要一个结果,其中所有正值都得到负id的值。
需要输出:
product quantity value id
---------------------------
p1 -100 5 id1
p2 40 5 id1
p2 20 5 id1
p2 40 5 id1
每个id只能有一个负值。每个id的所有项都必须得到负值的值。
答案 0 :(得分:2)
这应该这样做。
update the_table t1
set value = (select value
from the_table t2
where quantity < 0
and t2.id = t1.id)
where quantity > 0
and id = 'id1';
如果有多行且数量为&lt; ,则将失败 -1为给定的id