各位朋友,小弟的表如下:
ID 姓名   座号
1  张三
2  张五
3  张6
4  黄五
5  黄三
6  黄9

。现在想实现一个这样的UPDATE
记录从开始到结束,第一条记录的座号UPDATE为“AA”,第二条记录的座号UPDATE为“BB”,第三条记录的座号UPDATE为“CC”,然后第四条记录的座号UPDATE为“AA”,。就是说,每隔三条记录就循环一次,每次的UPDATE分别为:AA、BB、CC最后想要的结果:ID 姓名   座号
1  张三   AA
2  张五   BB
3  张6    CC
4  黄五   AA
5  黄三   BB
6  黄9    CC

。。
请问一下,这样能否有机会直接用SQL直接实现呢?请各位朋友赐教,谢谢 !!!

解决方案 »

  1.   

    update your_table set 座号 = case ID%3 when 0 then 'CC' when 1 then 'AA' when 2 then 'BB' end
      

  2.   

    update tb
    set 座号=case when ID%3=1 then 'AA' when ID%3=2 then 'BB' else 'CC' end
      

  3.   

    update tb
    set 座号=case ID%3 when 1 then 'AA' when 2 then 'BB' else 'CC' end
      

  4.   

    update tb
    set 座号 = case when id%3 = 1 then 'AA' when id%3 = 2 then 'BB' else 'cc' end
      

  5.   

    create table ttt(ID int,name varchar(10),seatNo varchar(10))
    insert ttt select 1,'张三',''
    union all select 2,'张五',''
    union all select 3,'张6',''
    union all select 4,'黄五',''
    union all select 5,'黄三',''
    union all select 6,'黄9',''update ttt set seatNo=case ID%3 when 1 then 'AAA' when 2 then 'BBB' when 0 then 'CCC' endselect * from ttt
      

  6.   

    declare @t table(ID int, 姓名 varchar(20),座号 varchar(20))
    insert @t(ID,姓名) select 1,'张三'
    insert @t(ID,姓名) select 2,'张五'
    insert @t(ID,姓名) select 3,'张6'
    insert @t(ID,姓名) select 4,'黄五'
    insert @t(ID,姓名) select 5,'黄三'
    insert @t(ID,姓名) select 6,'黄9';with tb as
    (
    select ROW_NUMBER() over(order by id) as rid,*
    from @t
    )update a
    set 座号=case b.rid%3 when 1 then 'AA' when 2 then 'BB' else 'CC' end
    from @t a join tb b on a.ID=b.IDselect * from @T
    /*
    ID          姓名                   座号
    ----------- -------------------- --------------------
    1           张三                   AA
    2           张五                   BB
    3           张6                   CC
    4           黄五                   AA
    5           黄三                   BB
    6           黄9                   CC
    */
      

  7.   


    create table your_table (ID int,姓名 nvarchar(3),座号 nvarchar(2))
    insert into your_table
    select 1, '张三',null union all
    select 2, '张五',null union all
    select 3, '张6',null union all
    select 4, '黄五',null union all
    select 5, '黄三',null union all
    select 6, '黄9',nullupdate your_table 
    set 座号 = case ID%3 when 0 then 'CC' 
                         when 1 then 'AA' 
                         when 2 then 'BB' 
               endselect * from your_table
    /*
    ID          姓名   座号
    ----------- ---- ----
    1           张三   AA
    2           张五   BB
    3           张6   CC
    4           黄五   AA
    5           黄三   BB
    6           黄9   CC(6 行受影响)*/
      

  8.   

    select 座号=(case when rn%3=1 then 'AA' when  rn%3=2 then 'BB'  else 'CC' end),* 
     from (select rn=row_number()over(order by filed1),* from tb) t1 
      

  9.   

    我晕啊,都是牛人,学习了,谢谢 啊其实自己也用过CASE和WHEN,就是不懂得灵活运用,谢谢各位大虾
      

  10.   

    id号如果为连续,可根据id号除3的余数来确定是AA,BB,CC,
    如果id号不连续,可创建一临时表变量生成连续的编号号对应原表id,根据生成的编号/3的余数来确定是
    AA,BB,还是CC
      

  11.   

    如果ID不连续
    declare @i int
    set @i=0
    update your_table set 
    @i=case when @i<3 then @i+1 else 1 end,
    座号=case when @i=1 then 'AA' when @i=2 then 'BB' when @i=3 then 'CC' end
      

  12.   

    declare @t table(ID int, 姓名 varchar(20),座号 varchar(20))
    insert @t(ID,姓名) select 1,'张三'
    insert @t(ID,姓名) select 2,'张五'
    insert @t(ID,姓名) select 3,'张6'
    insert @t(ID,姓名) select 4,'黄五'
    insert @t(ID,姓名) select 5,'黄三'
    insert @t(ID,姓名) select 6,'黄9';with tb as
    (
        select ROW_NUMBER() over(order by id) as rid,*
        from @t
     )update tb
    set 座号=case rid%3 when 1 then 'AA' when 2 then 'BB' else 'CC' end
    select * from @T
    /*
    ID          姓名                   座号
    ----------- -------------------- --------------------
    1           张三                   AA
    2           张五                   BB
    3           张6                   CC
    4           黄五                   AA
    5           黄三                   BB
    6           黄9                   CC(6 行受影响)
    */
      

  13.   


    create table t_2(ID int identity(1,1) not null, name nvarchar(max), seat nvarchar(max) NULL)
    insert t_2(name)
    select '张三' union all
    select '张五' union all
    select '张6'union all
    select '黄五' union all
    select '黄三' union all
    select '黄9'update t_2
    set seat=(case ID%3 when 1 then 'AA' when 2 then 'BB' when 0 then 'CC' end)select * from t_21 张三 AA
    2 张五 BB
    3 张6 CC
    4 黄五 AA
    5 黄三 BB
    6 黄9 CC