我是查询构建的新手。
我创建了两个表:
产品:
productid | name | price
命令:
id | productid | quantity | fullprice
当我添加新的记录值(productid、quantity)时,我能否自动计算全价(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
,则需要创建一个插入触发器,这会使您的架构更加复杂。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句