简体   繁体   English

插入带有计算列的语句

[英]Insert statement with calculated column

I have created the following simple DataModel : 我创建了以下简单的DataModel

在此处输入图片说明

I used the following insert statements to insert values: 我使用以下insert statements插入值:

1) Table Products : 1)餐桌产品

INSERT INTO test.products
(ProductName, Price)
VALUES 
("Product A","99,99"),
("Product B","49,95"), 
("Product C","5,95");

2) Table Orders : 2)表格顺序

INSERT INTO test.orders
(Customer)
VALUES 
("Customer A"),
("Customer B"), 
("Customer B");

All this works fine so far. 到目前为止,所有这些工作正常。


Now, in the table Products_per_Order I want to calculate the last column OrderValue by multiplying the Price with the Quantity . 现在,在表Products_per_Order我想通过将Price乘以Quantity来计算最后一列OrderValue Therefore, I tried to go with the following two options referring to the answers here but could not make them work yet: 因此,我尝试使用以下两个选项来引用此处的答案但仍无法使它们起作用:

Note: The column Price is getting the prices based on the idProduct from the table Products . 注意:Price ”列正在根据表ProductsidProduct获取价格。
This functionality already works. 此功能已起作用。 Only the multiplication with the quantity does not work. 仅与数量相乘不起作用。

Option 1: 选项1:

INSERT INTO test.products_per_order
(Orders_idOrders, Products_idProducts, Price, Quantity, OrderValue)
VALUES
("1","1",(Select Price from test.products where test.products.idProducts = "1"),"5",(Price * Quantity)),
("1","2",(Select Price from test.products where test.products.idProducts = "2"),"4",(Price * Quantity)),
("2","1",(Select Price from test.products where test.products.idProducts = "1"),"10",(Price * Quantity)),
("3","2",(Select Price from test.products where test.products.idProducts = "2"),"3",(Price * Quantity)),
("3","3",(Select Price from test.products where test.products.idProducts = "3"),"9",(Price * Quantity));

Option 2: 选项2:

INSERT INTO test.products_per_order 
(Orders_idOrders, Products_idProducts, Price, Quantity, OrderValue)
    SELECT m.idOrder, m.idProduct, p.price, m.qty, p.price * m.qty
    FROM (SELECT 1 as idOrder, 1 as idProduct, 5 as qty UNION ALL
          SELECT 1 as idOrder, 2 as idProduct, 4 as qty UNION ALL
          SELECT 2 as idOrder, 1 as idProduct, 10 as qty UNION ALL
          SELECT 3 as idOrder, 2 as idProduct, 3 as qty UNION ALL
          SELECT 3 as idOrder, 3 as idProduct, 9 as qty
         ) m
    LEFT JOIN test.products p on p.idProducts = m.idProduct;

Do you have any idea how to solve this issue? 您有解决该问题的想法吗?

You wrote that OrderValue is a calculated column, why not let the database calculate it for you? 您写道OrderValue是一个计算列,为什么不让数据库为您计算它呢?

Use alter table to change the OrderValue column to a generated column like this: 使用alter tableOrderValue列更改为生成的列,如下所示:

alter table test.products_per_order 
modify column OrderValue int generated always as (Price * Quantity) stored;

This way, you don't need to insert it at all - and your insert becomes like this: 这样,您根本不需要插入它-您的插入将变成这样:

INSERT INTO test.products_per_order 
(Orders_idOrders, Products_idProducts, Price, Quantity)
    SELECT m.idOrder, m.idProduct, p.price, m.qty
    FROM (SELECT 1 as idOrder, 1 as idProduct, 5 as qty UNION ALL
          SELECT 1 as idOrder, 2 as idProduct, 4 as qty UNION ALL
          SELECT 2 as idOrder, 1 as idProduct, 10 as qty UNION ALL
          SELECT 3 as idOrder, 2 as idProduct, 3 as qty UNION ALL
          SELECT 3 as idOrder, 3 as idProduct, 9 as qty
         ) m
    LEFT JOIN test.products p on p.idProducts = m.idProduct;

With reference to the comments below the question the issue is that the prices in the table Products need to be stored using . 参考以下问题的评论,问题是表Products中的prices需要使用来存储. as the decimal separator instead of , 作为小数分隔符代替,

If you modify the INSERT STATEMENT of the table Products to: 如果将表“ Products的“ INSERT STATEMENT ”修改为:

INSERT INTO test.products
(ProductName, Price)
VALUES 
("Product A","99.99"),
("Product B","49.95"), 
("Product C","5.95");

and then run the code in Option 2 everything works. 然后运行选项2中的代码,一切正常。

INSERT INTO test.products_per_order 
(Orders_idOrders, Products_idProducts, Price, Quantity, OrderValue)
    SELECT m.idOrder, m.idProduct, p.price, m.qty, p.price * m.qty
    FROM (SELECT 1 as idOrder, 1 as idProduct, 5 as qty UNION ALL
          SELECT 1 as idOrder, 2 as idProduct, 4 as qty UNION ALL
          SELECT 2 as idOrder, 1 as idProduct, 10 as qty UNION ALL
          SELECT 3 as idOrder, 2 as idProduct, 3 as qty UNION ALL
          SELECT 3 as idOrder, 3 as idProduct, 9 as qty
         ) m
    LEFT JOIN test.products p on p.idProducts = m.idProduct;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM