根据读出来Hit为1的值来更新 declare @ulr varchar(50) select @ulr=url from TB where hit=1 if @ulr='AAA' Begin update TB set Hit=0 where url='AAA' update TB set Hit=1 where url='BBB' End if @ulr='BBB' Begin update TB set Hit=0 where url='BBB' update TB set Hit=1 where url='CCC' End if @ulr='CCC' Begin update TB set Hit=0 where url='CCC' update TB set Hit=1 where url='AAA' End这样应该可以,但是是写死的,更新麻烦。
create table url (url char(3) ,hit tinyint)insert into url select 'AAA', 0 insert into url select 'BBB', 0 insert into url select 'CCC', 1 select *,identity(int,1,1) as id into #taoistong from urlselect * from #taoistongif (select count(1) from url )<>(select id from #taoistong where hit=1) begin update url set hit=1 from url a,#taoistong b where a.url=b.url and b.id=(select (id)+1 from #taoistong where hit=1) end else begin update url set hit=1 from url a,#taoistong B where a.url=b.url and b.id=(select MIN(id) from #taoistong) end
update url set hit=0 from url a,#taoistong b where a.url=b.url and b.hit=1select * from url
create table tb(id int identity(1,1) not null,Url varchar(10), Hit int) insert tb values('AAA', 0) insert tb values('BBB', 0) insert tb values('CCC', 1)select * from tb where hit=1declare @t table (id int) insert @t select id from tb where hit=1 if (select id from tb where hit=1)=(select max(id) from tb) insert @t select min(id) from tb else insert @t select top 1 id from tb where id >(select id from tb where hit=1)update tb set hit=(case when hit=0 then 1 else 0 end) where id in(select id from @t)select * from tb where hit=1
select row=row_number() over (order by url),* into # from tb declare @i int set @i=1 while (@i<10) begin declare @t int select @t=row from # where hit=1 update # set hit=0 where row=@t update # set hit=1 where row=case when @t=(select max(row) from #) then 1 else @t+1 end select * from # set @i=@i+1 end这里只给你循环了十次
我有办法了,你做个计数的变量,每变一次count=count+1select id=row_number() over (order by url),* into #Temp from tb declare @count int set @count=1--这是第一次 update #Temp set Hit=1 where @count%3=id update #Temp set Hit=1 where @count%3<>id set @count=2 update #Temp set Hit=1 where @count%3=id update #Temp set Hit=1 where @count%3<>id ......... 不管你循环多少次,每次只要传入这个@count就OK了。
有点错。select id=row_number() over (order by url),* into #Temp from tb declare @count int set @count=1--这是第一次 update #Temp set Hit=1 where @count%3=id update #Temp set Hit=0 where @count%3<>id set @count=2 update #Temp set Hit=1 where @count%3=id update #Temp set Hit=0 where @count%3<>id ...
根据读出来Hit为1的值来更新
declare @ulr varchar(50)
select @ulr=url from TB where hit=1
if @ulr='AAA'
Begin
update TB set Hit=0 where url='AAA'
update TB set Hit=1 where url='BBB'
End
if @ulr='BBB'
Begin
update TB set Hit=0 where url='BBB'
update TB set Hit=1 where url='CCC'
End
if @ulr='CCC'
Begin
update TB set Hit=0 where url='CCC'
update TB set Hit=1 where url='AAA'
End这样应该可以,但是是写死的,更新麻烦。
create table url
(url char(3)
,hit tinyint)insert into url select
'AAA', 0
insert into url select
'BBB', 0
insert into url select
'CCC', 1 select *,identity(int,1,1) as id
into #taoistong
from urlselect * from #taoistongif (select count(1) from url )<>(select id from #taoistong where hit=1)
begin
update url
set hit=1
from url a,#taoistong b
where a.url=b.url
and b.id=(select (id)+1 from #taoistong where hit=1)
end
else
begin
update url
set hit=1
from url a,#taoistong B
where a.url=b.url
and b.id=(select MIN(id) from #taoistong)
end
update url
set hit=0
from url a,#taoistong b
where a.url=b.url and b.hit=1select * from url
insert tb values('AAA', 0)
insert tb values('BBB', 0)
insert tb values('CCC', 1)select * from tb where hit=1declare @t table (id int)
insert @t select id from tb where hit=1
if (select id from tb where hit=1)=(select max(id) from tb)
insert @t select min(id) from tb
else
insert @t select top 1 id from tb where id >(select id from tb where hit=1)update tb set hit=(case when hit=0 then 1 else 0 end) where id in(select id from @t)select * from tb where hit=1
declare @i int
set @i=1
while (@i<10)
begin
declare @t int
select @t=row from # where hit=1
update #
set hit=0
where row=@t
update #
set hit=1
where row=case when @t=(select max(row) from #) then 1
else @t+1 end
select * from #
set @i=@i+1
end这里只给你循环了十次
declare @count int
set @count=1--这是第一次
update #Temp set Hit=1 where @count%3=id
update #Temp set Hit=1 where @count%3<>id
set @count=2
update #Temp set Hit=1 where @count%3=id
update #Temp set Hit=1 where @count%3<>id
.........
不管你循环多少次,每次只要传入这个@count就OK了。
declare @count int
set @count=1--这是第一次
update #Temp set Hit=1 where @count%3=id
update #Temp set Hit=0 where @count%3<>id
set @count=2
update #Temp set Hit=1 where @count%3=id
update #Temp set Hit=0 where @count%3<>id
...
for(i=0;i<3;i++){
if(hit[i]==1){
switch(i){
case 0 : printf( url[1] ); hit[1]=1; hit[0]=0;
break;
case 1 : printf( url[2] ); hit[2]=1; hit[1]=0;
break;
case 2 : printf( url[0] ); hit[0]=1; hit[2]=0;
break;
}
}
我是告诉你方法,剩下的你自己调试,我要去睡觉了。
void Done(char *url,int *hit, int cnt)
{
int i, j;
for(i=0;i <cnt; i++)if(hit[i]==1){
j= i==cnt-1? 0:i+1;
printf( url[j] ); hit[j]=1; hit[i]=0;
break;
}
}读书不为功名,闲来也作小诗。