SQL 语句 求和

lakibo 2006-11-24 02:41:20
Table Products : TID , cost
Table Parts : PID , TID , quantity

已知 PID , 从 表 Parts 中获取 list of TID ,求和 cost

Sum = Sum(cost * quantity)

谢谢了
...全文
271 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
懒得去死 2006-11-27
  • 打赏
  • 举报
回复
select parts.pid, sum(IFNull(products.cost,0)*parts.quantity) from parts left join products on parts.tid=products.tid group by parts.pid having parts.pid='somepid'
mschen 2006-11-25
  • 打赏
  • 举报
回复
/* mysql里边应该使用IFNULL */

select parts.pid,
sum(IFNull(products.cost,0)*parts.quantity)
from parts left join products
on parts.tid=products.tid
group by parts.pid
having parts.pid='somepid'
lakibo 2006-11-24
  • 打赏
  • 举报
回复
理论上

select sum(products.cost * parts.quantity) from products where TID = { select TID from parts where PID = 'somepid' }

有许多 TID
CaiNiaoWuZui 2006-11-24
  • 打赏
  • 举报
回复
select parts.pid,sum((isNull(products.cost),0))*parts.quantity) from parts left join products on parts.tid=products.tid group by parts.pid
lakibo 2006-11-24
  • 打赏
  • 举报
回复
不行阿。
caixia615 2006-11-24
  • 打赏
  • 举报
回复
SELECT pid,sum(products.cost*parts.quantity) from products,parts where products.tid=parts.tid

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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