select name ,case when name = 'a' then '1' else '-1' end as Num from students
update students set 字段b=(case when 字段a=a then 1 else -1 end)
update students set 字段2=1 where 字段1='a' update students set 字段2=-1 where 字段1<>'a'
--> 数据库版本: --> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 --> 测试数据:students IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'students') AND type in (N'U')) DROP TABLE students GO---->建表 create table students([name] varchar(1),[value] int) insert students select 'a',0 union all select 'b',0 union all select 'c',0 union all select 'd',0 union all select 'e',0 GOupdate students set value=-1 where name<>'a'--> 查询结果 SELECT * FROM students --> 删除表格 --DROP TABLE students
修改就用 UPDATE,查询就用select
UPDATE students SET number = (CASE WHEN [name] = 'a' THEN 1 ELSE -1 END)
update students set 字段b=(case when 字段a=a then 1 else -1 end)
update students set 字段2=1 where 字段1='a'
update students set 字段2=-1 where 字段1<>'a'
--> 数据库版本:
--> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
--> 测试数据:students
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'students')
AND type in (N'U'))
DROP TABLE students
GO---->建表
create table students([name] varchar(1),[value] int)
insert students
select 'a',0 union all
select 'b',0 union all
select 'c',0 union all
select 'd',0 union all
select 'e',0
GOupdate students
set value=-1
where name<>'a'--> 查询结果
SELECT * FROM students
--> 删除表格
--DROP TABLE students
SET number = (CASE WHEN [name] = 'a' THEN 1 ELSE -1 END)