news
id   title  count
1     aaaa   0
2     bbbb   0
3     cccc   0以上表结构,通过缓存记录了每条文章的点击ID,然后定时更新文章点击数,SQL语句如何写性能最高?如记录了以下 1,2,1,1,3,3
通过update news set count=count+1 where id in (1,2,1,1,3,3)
重复相同ID只能+1,如何才能实现
id=1的三条+3
id=2的一条则+1
id=3的二条则=2

解决方案 »

  1.   

    declare @news table(id int,title varchar(10),[count] int)
    insert into @news select 1,'aaaa',0 
    insert into @news select 2,'bbbb',0 
    insert into @news select 3,'cccc',0 declare @str varchar(40)
    set @str='1,2,1,1,3,3'  update t set [count]=[count]+(len(','+replace(@str,',',',,')+',')-len(replace(','+replace(@str,',',',,')+',' , ','+rtrim(id)+',' , ',,')))/len(id) from @news tselect * from @news
    /*
    id          title      count       
    ----------- ---------- ----------- 
    1           aaaa       3
    2           bbbb       1
    3           cccc       2
    */