如何提出字符串?請大家幫個忙

aderly 2005-08-30 02:13:52
比如 有一條記錄是 b,c,d
有一個表 a 10
b 15
c 16
d 17
. .
. .
我要得到這條記錄的和 上面b,c,d的和為 15+16+17=48
不知道大家懂了嗎?謝謝
...全文
166 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
MorningTea 2005-08-30
  • 打赏
  • 举报
回复
比如 有一條記錄是 b,c,d ---这句话是什么意思,是来自另外一个表的一笔记录吗?
有一個表 a 10
b 15
c 16
d 17
. .
. .
我要得到這條記錄的和 上面b,c,d的和為 15+16+17=48
不知道大家懂了嗎?謝謝
GRLD8888 2005-08-30
  • 打赏
  • 举报
回复
create table table1 (id varchar(8),num int )
insert table1 select 'a',10
union all select 'b',15
union all select 'c',16
union all select 'd',17


select sum(num) from table1 where id in ('b','c','d')
vivianfdlpw 2005-08-30
  • 打赏
  • 举报
回复
declare @str varchar(20)
set @str='b,c,d'

select sum(统计字段) from 表 where charindex(','+字段+',',','+@str+',')>0
zlp321002 2005-08-30
  • 打赏
  • 举报
回复
--测试环境
declare @t table (x varchar(2),y int)
insert into @t select 'a',10
union all select 'b',15
union all select 'c',16
union all select 'd',17

--统计
select 结果=sum(Y) from @t where charindex(','+x+',',',b,c,d,')>0

--结果
结果
-----------
48

(所影响的行数为 1 行)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧