Sql 将两个表中的信息添加到第二个表中
我是查询大楼的新手 我创建了两个表: 产品:Sql 将两个表中的信息添加到第二个表中,sql,postgresql,inner-join,Sql,Postgresql,Inner Join,我是查询大楼的新手 我创建了两个表: 产品: productid | name | price 订单: id | productid | quantity | fullprice 当我添加新的记录值(productid,quantity)时,是否可以自动计算fullprice(product.price*order.quantity)?您可以加入: select p.*, o.quantity, o.quantity * p.price as fullprice from products
productid | name | price
订单:
id | productid | quantity | fullprice
当我添加新的记录值(productid,quantity)时,是否可以自动计算fullprice(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
,则需要创建一个insert触发器,这会使模式更加复杂