有一students表
a   0
b   0
c   0
d   0
e   0
现用sql实现:除a同学变成1外,其他同学都变成-1.我感觉很简单啊,就是脑袋不好用

解决方案 »

  1.   

    select name ,case when name = 'a' then '1' else '-1' end as Num from students
      

  2.   

    update students set 字段b=(case when 字段a=a then 1 else -1 end)
      

  3.   


    update students set 字段2=1 where 字段1='a'
    update students set 字段2=-1 where 字段1<>'a'
      

  4.   


    --> 数据库版本:
    --> 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
      

  5.   

    修改就用 UPDATE,查询就用select 
      

  6.   

    UPDATE students
    SET number = (CASE WHEN [name] = 'a' THEN 1 ELSE -1 END)
      

  7.   

    update students set 字段b=(case when 字段a=a then 1 else -1 end)