各位朋友,小弟的表如下:
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直接实现呢?请各位朋友赐教,谢谢 !!!
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直接实现呢?请各位朋友赐教,谢谢 !!!
set 座号=case when ID%3=1 then 'AA' when ID%3=2 then 'BB' else 'CC' end
set 座号=case ID%3 when 1 then 'AA' when 2 then 'BB' else 'CC' end
set 座号 = case when id%3 = 1 then 'AA' when id%3 = 2 then 'BB' else 'cc' end
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
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
*/
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 行受影响)*/
from (select rn=row_number()over(order by filed1),* from tb) t1
如果id号不连续,可创建一临时表变量生成连续的编号号对应原表id,根据生成的编号/3的余数来确定是
AA,BB,还是CC
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
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 行受影响)
*/
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