mysql 相乘,MySQL的:如何求和相乘的部分

Having tables like:

sold_items:

code | size | quantity

abc123 | small | 4

abc123 | medium | 2

xyz987 | small | 3

xyz987 | medium | 1

xyz987 | large | 2

price_list:

code | size | price

abc123 | small | 5

abc123 | medium | 7

abc123 | large | 9

xyz987 | small | 10

xyz987 | medium | 13

xyz987 | large | 15

which would be your best (faster query) approach to get

results:

code | sold_items | cash

abc123 | 6 | 34

xyz987 | 6 | 73

解决方案

This should work:

SELECT si.code, SUM(si.quantity), SUM(si.quantity * pl.price) as cash

FROM sold_items si

INNER JOIN price_list pl ON pl.code = si.code AND pl.size = si.size

GROUP BY si.code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值