问题描述
|
我有一个表,其中有列名课程。第二行的课程是\“ C ++ \”,第四行的课程是\“ ASP.net \”。
我想将其与更新查询交换价值。我该如何实现?
解决方法
您可以使用
update
更改值,例如:
update YourTable set Course = \'ASP.NET\' where id = 2
update YourTable set Course = \'C++\' where id = 4
要么:
update YourTable
set Course =
case id
when 2 then \'ASP.NET\'
when 4 then \'C++\'
end
where id in (2,4)
,测试表和数据
create table YourTable(id int primary key,course varchar(10))
insert into YourTable values (1,\'Delphi\')
insert into YourTable values (2,\'C++\')
insert into YourTable values (3,\'Clipper\')
insert into YourTable values (4,\'ASP.net\')
更新到开关2和4
update YourTable set
course = case id
when 4 then (select course from YourTable where id = 2)
when 2 then (select course from YourTable where id = 4)
else T.course
end
from
YourTable as T
where T.id in (2,4)
结果
id course
1 Delphi
2 ASP.net
3 Clipper
4 C++