我有一个表,其中有A,B,C列。
如果A和B(A,B一起是唯一的),我想更新C列,因此伪代码如下:
python get-pip.py
如何用SQL编写此代码?
答案 0 :(得分:2)
我认为您可以使用case
表达式:
update table
set c = (case when a = 1 and b = 1 then 1000
when a = 2 and b = 2 then 2000
when a = 3 and b = 3 then 3000
end)
where (a = 1 and b = 1) or (a = 2 and b = 2) or (a = 3 and b = 3);
答案 1 :(得分:1)
多个更新都可以在任何RDBMS中使用
update yourtable set c = 1000 where a = 1 and b = 1
update yourtable set c = 2000 where a = 2 and b = 2
update yourtable set c = 3000 where a = 3 and b = 3
人们会假设UPDATE语句应该非常标准。
但是,从表或子查询进行更新时,语法可能会有所不同。
这在MS Sql Server中有效
update t
set c = q.c
from yourtable t
join (values
(1, 1, 1000)
,(2, 2, 2000)
,(3, 3, 3000)
) q(a, b, c)
on t.a = q.a and t.b = q.b
这在Postgresql中有效
update yourtable t
set c = q.c
from
(values
(1, 1, 1000)
,(2, 2, 2000)
,(3, 3, 3000)
) q(a, b, c)
where q.a = t.a and q.b = t.b
这在MySql中有效
update yourtable t
join
(
select 1 as a, 1 as b, 1000 as c
union all select 2, 2, 2000
union all select 3, 3, 3000
) q on q.a = t.a and q.b = t.b
set t.c = q.c
这在Oracle RDBMS中有效
update yourtable t
set t.c =
(
select q.c
from
(
select 1 as a, 1 as b, 1000 as c from dual
union all select 2, 2, 2000 from dual
union all select 3, 3, 3000 from dual
) q
where q.a = t.a and q.b = t.b
)
答案 2 :(得分:0)