如何加入每个订单及其价格?

问题描述

我有两个名为 PRICES 和 ORDERS 的表。

价格

Product_ID 价格
1 50
2 100

订单

CUSTOMER_ID Product_ID 数量
123 1 3
456 2 5
789 2 2
327 1 7

我想加入这两个表以获得低于预期的输出

预期输出

Product_ID CUSTOMER_ID 数量 价格 总计
1 123 3 50 150
2 456 5 100 500
2 789 2 100 200
1 327 7 50 350

解决方法

您可以使用内连接获取订单表中每个产品的价格:

Select o.customer_id,p.product_id,o.quantity,p.price
from PRICES p inner join ORDERS o on p.product_id=o.product_id