declare @Photoid varchar(1000)
set @Photoid='1,2,3,4,5,6,7,8,9,'
update photoclass set piccount=100 where  charindex(','+rtrim(id)+',' , ','+@Photoid+',')>0 这条语句大家都能看懂吧? 是修改photoclass 表中ID为1,2,3,4,5,6,7,8,9,的数据,将piccount=100,
下面我要加个功能。
有个photodetail图片详细表,里面有个Classid,关系为:photoclass.id=photodetail.classid 
我要实现的功能是计算classid=@Photoid='1,2,3,4,5,6,7,8,9,' 的数据条数
然后photoclass.piccount=这个条数说白了就是 批量修改图片类别表里图片数量 条件是图片详细表的类别=传入的ID组合本人菜鸟 希望大侠修改代码 完整一点!!

解决方案 »

  1.   

    我只会写单条的修改:
    declare @id int
    set @id=1
    update photoclass set piccount=(select count(*) from photodetail where classid=@id
    where id=@id 但是批量传入的ID 比如说:
    declare @Photoid varchar(1000)
    set @Photoid='1,2,3,4,5,6,7,8,9,'
    我就不知道怎么写了。
    分不多 大侠们多帮忙
      

  2.   

    --tryupdate photoclass set piccount=count(photodetail.*)
    from photodetail
    where photoclass.id=photodetail.classid  and
    charindex(','+rtrim(photodetail.classid)+',', ','+@Photoid+',')>0
      

  3.   

    在2005就没问题,在2000或2000以下就会有问题
    @Photoid列变量不支持
      

  4.   

    declare @Photoid varchar(1000)
    set @Photoid='1,2,3,4,5,6,7,8,9,'update photoclass set piccount=(select count(*) from photodetail where @Photoid like '%'+classid+'%' where @Photoid  like '%'+id +'%'
      

  5.   

    我用的SQL2000 大侠们先看清我的需求
      

  6.   


    declare @Photoid varchar(1000)
    set @Photoid='1,2,3,4,5,6,7,8,9,'select a.*
    from (select 1 as id union all
    select 2 union all
    select 3 union all
    select 4 union all
    select 5 union all
    select 6 union all
    select 7 union all
    select 8 union all
    select 9 )a
    where charindex(','+rtrim(a.id)+',',','+@Photoid)>0
    id          
    ----------- 
    1
    2
    3
    4
    5
    6
    7
    8
    9(所影响的行数为 9 行)
      

  7.   

    @Photoid 是变量 !!可能是100多个ID的组合 @Photoid='1,2,3,4,5................110,'
      

  8.   

    create table ta(id int identity(1,1),name varchar(2))
    id          name 
    ----------- ---- 
    1           a
    2           a
    3           a
    4           a
    5           a
    6           a
    7           a
    8           a
    9           a
    10          a
    11          a
    12          a
    13          a
    14          a
    15          a
    16          a
    17          a
    18          a(所影响的行数为 18 行)declare @Photoid varchar(1000)
    set @Photoid='1,2,3,4,5,6,7,8,9,'update ta
    set name=(select count(*)from ta where charindex(','+rtrim(ta.id)+',',','+@Photoid)>0)
    where charindex(','+rtrim(ta.id)+',',','+@Photoid)>0select * from ta
    id          name 
    ----------- ---- 
    1           9
    2           9
    3           9
    4           9
    5           9
    6           9
    7           9
    8           9
    9           9
    10          a
    11          a
    12          a
    13          a
    14          a
    15          a
    16          a
    17          a
    18          a(所影响的行数为 18 行)
      

  9.   

    我这个是有2张表photoclass,photodetail
    关系:photoclass.id=photodetail.classid 
    如果photoclass.id=1
    我要查出photodetail.classid=1的数据总数 并把photoclass.piccount=数据总数我要实现批量修改所以定义了@Photoid
    @Photoid就是 photoclass.id的集合 比如:'1,2,3,4,5,6,'
    所以我用:charindex(','+rtrim(id)+',',','+@Photoid)>0 来拆开@Photoid具体意思您明白了吗?
      

  10.   

    declare @id int
    set @id=1
    update photoclass set piccount=(select count(*) from photodetail where classid=@id
    where id=@id 
    以上是单条数据修改。
    我现在就是想换成批量修改 
    接收个id组合@Photoid
    如:
    declare @Photoid varchar(1000)
    set @Photoid='1,2,3,4,5,6,7,8,9,'
    update photoclass set piccount=100 where  charindex(','+rtrim(id)+',' , ','+@Photoid+',')>0
    piccount=100应该换成什么呢???????????????
      

  11.   


    类别表photoclass ,ID为主键,piccount为此类别的数据总数
    .................................
    .ID                   piccount  .
    .................................                    
    .1                       100    .
    .2                       100    .
    .3                       100    .
    .4                       100    .
    .5                       100    .
    .6                       100    .
    .................................
    数据表photodetail, ID为主键,Classid为类别ID,对应于photoclass表的主键ID
    .................................
    .ID                   classid   .
    .................................                    
    .1                       1      .
    .2                       1      .
    .3                       2      .
    .4                       2      .
    .5                       2      .
    .6                       3      .
    .7                       4      .
    .8                       4      .
    .9                       5      .
    .................................现在我用以下语句操作:
    declare @id int
    set @id=1
    update photoclass set piccount=(select count(*) from photodetail where classid=@id)
    where id=@id 不难看出 类别表photoclass ,ID为1的 piccount字段的值会被改成2。
    .................................
    .ID                   piccount  .
    .................................                    
    .1                       2      .
    .2                       100    .
    .3                       100    .
    .4                       100    .
    .5                       100    .
    .6                       100    .
    .................................
    这只是操作单条数据。我现在要批量修改
    那么需要接收一个类别表photoclass 的ID组
    比如说:@Photoid='1,2,3,4,5,6,'。需要注意的是@Photoid是变量参数可能是:'1,2,3,4,5,6,7,8,9,10,...................1000' 或更多。根据数据表photodetail的数据
    如何写这个语句才能让类别表photoclass数据如下呢?
    .................................
    .ID                   piccount  .
    .................................                    
    .1                       2      .
    .2                       3      .
    .3                       1      .
    .4                       2      .
    .5                       1      .
    .6                       0      .
    .................................
      

  12.   

    declare @id varchar(8000)
    set @id='1,2,3,4,5,6'
    update a set piccount=(select count(*) from photodetail where classid=a.id)
    from photoclass a
    where charindex(','+rtrim(id)+',' , ','+@id+',')>0
      

  13.   

    借楼上的代码改一下
    declare @id varchar(8000)
    set @id='1,2,3,4,5,6'
    update photoclass set piccount=cnt
    from (select classid,count(*) as cnt from photodetail where charindex(','+rtrim(id)+',' , ','+@id+',')>0 group by classid)b
    where photoclass .id=b.classid
      

  14.   

    看看这个语句对你有没有帮助declare @id int
    set @id=1
    update photoclass set photoclass.piccount=classid
    where
    photoclass.piccount in (select classid  from  photodetail where classid=@id group by classid having count(1)>0)
      

  15.   

    t1表
    create table t1(id int,name int)
    -------------------------------------------------------
    declare @Photoid varchar(1000)
    ,@hh int
    set @Photoid='1,2,3,4,5,6,7,8,9,10'
    select convert(int,left(@photoid,charindex(',',@photoid)-1))
    create table #t1(id int)while len(@photoid)>0
    begin
    if charindex(',',@photoid)>0
    begin
    set @hh=convert(int,left(@photoid,charindex(',',@photoid)-1))
    insert into #t1 values(@hh)
    set @photoid=substring(@photoid,charindex(',',@photoid)+1,len(@photoid)-charindex(',',@photoid))
    end
    else
    begin
    insert into #t1 values(@photoid)
    set @photoid=''
    end
    end
    update t1 set name=100 where id in (select * from #t1)
    drop table #t1