问个sql语句

lunat 2010-05-20 05:10:04
有一张表记录配件价格 products(id,price)

现在我有个字符串表示的配件包 比如 "5,2,3,4,4,5,6,8,8,6",其中的每个数字对应products中的id(产品编号)
现在 我想计算整个配件包的价格 怎么写sql语句?

access2000 谢谢
...全文
295 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
goutao1124 2010-05-21
  • 打赏
  • 举报
回复
学习学习
我是-稻草人 2010-05-21
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
开发者孙小聪 2010-05-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zltgb2000 的回复:]

一句sql搞不定,可以先用select * from products where id in (5,2,3,4,4,5,6,8,8,6)查询出附件包内附件单价,然后再按每种附件个数累计求和。
[/Quote]

zhengjie
sunchen891229 2010-05-20
  • 打赏
  • 举报
回复
路过,学习了~~
lunat 2010-05-20
  • 打赏
  • 举报
回复
感谢 zltgb2000 以及20楼 21楼的热心帮助

谢谢25、26等各位来水…… 感谢cctv/mtv/所有的v……

结果是两个方案,我还要测试一下:

1.直接查询出每种配件的价格,然后在程序中在字符串中检查每种配件的个数,循环计算出整个配件包的价格
2.采用20楼的朋友提供的拼sql的办法
jianshao810 2010-05-20
  • 打赏
  • 举报
回复
select sum(*) from products where id in (5,2,3,4,4,5,6,8,8,6);
xk1126 2010-05-20
  • 打赏
  • 举报
回复
select sum(price) from products where id in (5,2,3,4,4,5,6,8,8,6);
lunat 2010-05-20
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 chjpeng 的回复:]

拼sql
注:access 2003测试通过
select sum(price) from (
select price from products where id=5
union
select price from products where id=2
union
select price from products where id=3
union
se……
[/Quote]
这个可以啊 …… 不知道效率如何……
lunat 2010-05-20
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zzxap 的回复:]

select sum(price) from products where id in (5,2,3,4,4,5,6,8,8,6);

配件包由重复的配件组成,有重复id才对啊。
[/Quote]

这样查出来的结果只是所有种类的配件 每种一个的价格之和 而不是整个配件包的价格

“5,2,3,4,4,5,6,8,8,6” 中包含 1个“2” 1个“3” 2个“4” 2个“5” 2个“6” 2个“8”
必须是
price(id=2) + price(id=3) + 2*price(id=4) + 2*price(id=5) + 2*price(id=6) + 2*price(id=8)

这样算出来的结果才是我想要的……
derxing 2010-05-20
  • 打赏
  • 举报
回复
select * from products where id = "5,2,3,4,4,5,6,8,8,6"
ljjk123 2010-05-20
  • 打赏
  • 举报
回复
access2000 不支持 split....
方法1
如楼上 拼sql
2 升级到 access 2007
3 用access 自身带的模块 实现split
然后做查询
直接查 查询结果
chjpeng 2010-05-20
  • 打赏
  • 举报
回复
拼sql
注:access 2003测试通过
select sum(price) from (
select price from products where id=5
union
select price from products where id=2
union
select price from products where id=3
union
select price from products where id=4
union
select price from products where id=4
...
);
simon_c 2010-05-20
  • 打赏
  • 举报
回复

select sum(a.price) from (select ditinct id,price from products where id in (5,2,3,4,4,5,6,8,8,6)) a
garfieldzf 2010-05-20
  • 打赏
  • 举报
回复
select sum(distinct(price)) from products

这是我在sql2005中写的
你看看在Access中能运行不。
碎甜菜 2010-05-20
  • 打赏
  • 举报
回复
经测试,上面所有一句sql的解决办法都不可行。
mingqi_pan 2010-05-20
  • 打赏
  • 举报
回复
理解错了。
ljjk123 2010-05-20
  • 打赏
  • 举报
回复
才看到是 access2000.....
改下
select sum(price) from products a ,(select split('5,2,3,4,4,5,6,8,8,6',',') id from products) b where a.id=b.id
cluw_love 2010-05-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhuazhuqingchong 的回复:]
select sum(price) from products where id='5'or id='2'or id='3'......
[/Quote]
这个也许行吧?
mingqi_pan 2010-05-20
  • 打赏
  • 举报
回复
select isnull(sum(price),0) price from ( select distinct id,price from products where id in (5,2,3,4,4,5,6,8,8,6)) A
zzxap 2010-05-20
  • 打赏
  • 举报
回复
select sum(price) from products where id in (5,2,3,4,4,5,6,8,8,6);

配件包由重复的配件组成,有重复id才对啊。
加载更多回复(11)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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