将两个 table 的信息添加到第二个 table

Add information from two tables to second table

我是查询构建方面的新手。

我创建了两个表:

产品:

productid | name | price

订单:

id | productid | quantity | fullprice

当我添加新记录值(productid、数量)时,我可以自动计算全价(product.price * order.quantity)吗?

您可以加​​入:

select p.*, o.quantity, o.quantity * p.price as fullprice
from products p
inner join orders o on o.productid = p.productid

我不建议存储 fullprice。这是派生信息,可以在需要时使用上述查询即时计算。如果您要经常使用查询,您可能需要创建一个视图:

create view v_product_orders as
select p.*, o.quantity, o.quantity * p.price as fullprice
from products p
inner join orders o on o.productid = p.productid

如果要存储 fullprice,则需要创建一个插入触发器,这会使您的架构更加复杂。