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组合本人菜鸟 希望大侠修改代码 完整一点!!
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组合本人菜鸟 希望大侠修改代码 完整一点!!
解决方案 »
- ADO连接SQL数据库设置使用字符集的参数
- 【100分】 求 一 条 简 单 的 sql 语 句
- 大数据表如何统计行数字?count(*)不行了。
- SQL 更新记录的问题
- 这个存储过程为什么出现“将 varchar 值 '61,62,63' 转换为数据类型为 int 的列时发生语法错误。”
- 好奇怪的数据库连接问题
- timestamp类型的数据无法在数据库订阅同步模式下使用,如何解决,再送200分
- 看看合理吗
- 看看这句,有什么错误?
- sql 7.0的复制为什么快照代理报错:Another snapshot agent for the publication is running
- 问一个事务隔离级别和锁的概念问题
- 简单查询问题送100分,快来抢分了啊
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,'
我就不知道怎么写了。
分不多 大侠们多帮忙
from photodetail
where photoclass.id=photodetail.classid and
charindex(','+rtrim(photodetail.classid)+',', ','+@Photoid+',')>0
@Photoid列变量不支持
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 +'%'
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 行)
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 行)
关系: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具体意思您明白了吗?
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应该换成什么呢???????????????
类别表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 .
.................................
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
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
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)
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