Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将两个表中的信息添加到第二个表中_Sql_Postgresql_Inner Join - Fatal编程技术网

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触发器,这会使模式更加复杂