简体   繁体   English

MYSQL:View语句产生不正确的SUM总计

[英]MYSQL: View statement produces incorrect SUM totals

I have 3 tables. 我有3张桌子。 A table for product prices, invoiced products, and ordered products. 产品价格,发票产品和订购产品的表格。 I am trying to create a view that joins these. 我正在尝试创建一个将这些结合在一起的视图。 I want to output the product prices with a total of invoiced products and a total of ordered products. 我想输出包含发票产品和订购产品总数的产品价格。

products_price 产品价格

id  season_id   product_id  product_price
1   1           1           3.99
2   1           2           6.99
3   1           3           5.99
4   1           4           5.99
....

invoices_products 发票_产品

id  season_id   invoice_id  product_id  piece_qty
1   1           1           1           1600
2   1           2           2           3200
3   1           3           2           200
4   1           4           1           120 
....

orders_products 订单_产品

id  season_id   order_id    product_id  piece_qty
1   1           1           1           160
2   1           2           1           40
3   1           2           2           20
4   1           3           2           10
....

Here are a few queries from the View statements I've tried so far. 这是到目前为止我尝试过的一些View语句查询。

This query gives me everything I want. 这个查询给了我我想要的一切。 The View's output is perfect but the SUM() for the first 2 rows is off. 视图的输出是完美的,但前两行的SUM()已关闭。 total_invoice_product is double for row 1 and 2. total_order_product is 4x for row 1 and 3x for row 2. 第1行和第2行的total_invoice_product为两倍。第1行的total_order_product为4x,第2行为3x。

Statement 1: 陈述1:

SELECT 
    `t1`.`id` AS `id`,
    `t1`.`season_id` AS `season_id`,
    `t1`.`product_id` AS `product_id`,
    `t1`.`product_piece_price` AS `product_piece_price`,
    SUM(`t2`.`piece_qty`) AS `total_invoice_product`,
    SUM(`t3`.`piece_qty`) AS `total_order_product`
FROM
    ((`products_price` `t1`
    LEFT JOIN `invoices_products` `t2` ON (((`t2`.`product_id` = `t1`.`product_id`)
        AND (`t2`.`season_id` = `t1`.`season_id`))))
    LEFT JOIN `orders_products` `t3` ON (((`t3`.`product_id` = `t1`.`product_id`)
        AND (`t3`.`season_id` = `t1`.`season_id`))))
GROUP BY `t1`.`season_id` , `t1`.`product_id`

This query gives me the output that I expect. 该查询给了我期望的输出。 Its not the full output I want but its correct for the statement. 它不是我想要的完整输出,但对于语句来说是正确的。 The SUM() totals are off on this one as well. SUM()总数也与此有关。

Statement 2: 陈述2:

SELECT 
    `t1`.`id` AS `id`,
    `t1`.`season_id` AS `season_id`,
    `t1`.`product_id` AS `product_id`,
    `t1`.`product_price` AS `product_price`,
    SUM(`t2`.`piece_qty`) AS `total_invoice_product`,
    SUM(`t3`.`piece_qty`) AS `total_order_product`
FROM
    ((`products_price` `t1`
    LEFT JOIN `invoices_products` `t2` ON ((`t2`.`product_id` = `t1`.`product_id`)))
    LEFT JOIN `orders_products` `t3` ON ((`t3`.`product_id` = `t1`.`product_id`)))
WHERE
    ((`t2`.`season_id` = `t1`.`season_id`)
        AND (`t2`.`product_id` = `t1`.`product_id`))
GROUP BY `t1`.`season_id` , `t1`.`product_id`

the output I want 我想要的输出

id  season_id   product_id  product_price   total_invoice   total_order
1   1           1           3.99            1720            200
2   1           2           6.99            3400            30
3   1           3           5.99            576 
4   1           4           5.99            800 

output received for statement 1 语句1收到的输出

id  season_id   product_id  product_price   total_invoice   total_order
1   1           1           3.99            3440            800
2   1           2           6.99            6800            90
3   1           3           5.99            576 
4   1           4           5.99            800 

output received for statement 2 语句2收到的输出

id  season_id   product_id  product_price   total_invoice   total_order
1   1           1           3.99            3440            800
2   1           2           6.99            6800            90

I can build a query like below and it works perfect. 我可以建立一个如下所示的查询,它可以完美地工作。 I get the exact output I need but this code does not work as a view. 我得到了所需的确切输出,但是此代码无法用作视图。 I get this error: ERROR 1349: View's SELECT contains a subquery in the FROM clause SQL Statement 我收到此错误: 错误1349:视图的SELECT在FROM子句SQL语句中包含一个子查询

Perfect Query but will not work as a view 完美查询,但无法用作视图

SELECT 
    products_price.id,
    products_price.season_id,
    products_price.product_id,
    products_price.product_price,
    invoices_grouped.total_invoice_product,
    orders_grouped.total_order_product
FROM
    products_price
LEFT JOIN
    (SELECT 
        invoices_products.product_id,
        invoices_products.season_id,
        SUM(invoices_products.piece_qty) AS total_invoice_product
    FROM
        invoices_products
    GROUP BY 
        invoices_products.product_id) AS invoices_grouped 
    ON 
        invoices_grouped.product_id = products_price.product_id
    AND
        invoices_grouped.season_id = products_price.season_id
LEFT JOIN
    (SELECT 
        orders_products.product_id,
        orders_products.season_id,
        SUM(orders_products.piece_qty) AS total_order_product
    FROM
        orders_products
    GROUP BY 
        orders_products.product_id) AS orders_grouped
    ON 
        orders_grouped.product_id = products_price.product_id
    AND
        orders_grouped.season_id = products_price.season_id

What I need 我需要的

I've tried several other statements. 我已经尝试了其他一些陈述。 They either got worse results or the same. 他们要么得到更差的结果,要么相同。 Can someone help me get Statement 1 working with a proper SUM? 有人可以帮我得到正确的SUM陈述1吗?

Edit 1 for a question 为问题编辑1

The information that this view provides will be called upon a lot. 该视图提供的信息将被大量调用。 The products_price and invcoices_products tables will not be changed that often. products_price和invcoices_products表不会经常更改。 orders_products will be changed a lot. orders_products将发生很大变化。 If 2 views are required, would it be more efficient to use the "Perfect" query above or use 2 views? 如果需要2个视图,使用上面的“ Perfect”查询或使用2个视图会更有效吗?

Edit 2 for another query 编辑2进行另一个查询

Here is another query from my view statement. 这是我的视图语句中的另一个查询。 This query is part of Statement 1 shown above. 该查询是上面显示的语句1的一部分。 This query works perfect but it is not complete. 此查询工作完美,但不完整。 I need the second SUM column. 我需要第二个SUM列。 When you add the second LEFT JOIN it breaks the SUM totals. 当您添加第二个LEFT JOIN时,它将破坏SUM总数。

SELECT 
    `t1`.`id` AS `id`,
    `t1`.`season_id` AS `season_id`,
    `t1`.`product_id` AS `product_id`,
    `t1`.`product_piece_price` AS `product_piece_price`,
    SUM(`t2`.`piece_qty`) AS `total_invoice_product`
FROM
    (`products_price` `t1`
    LEFT JOIN `invoices_products` `t2` ON (((`t2`.`product_id` = `t1`.`product_id`)
        AND (`t2`.`season_id` = `t1`.`season_id`))))
GROUP BY `t1`.`season_id` , `t1`.`product_id`

output 产量

id  season_id   product_id  product_price   total_invoice   
1   1           1           3.99            1720            
2   1           2           6.99            3400            
3   1           3           5.99            576 
4   1           4           5.99            800 

Well, MySql has some limitations, so you will need to create 2 views for subqueries and use them: 好吧,MySql有一些限制,因此您将需要为子查询创建2个视图并使用它们:

create view viewInvoices
as
select season_id, product_id, sum(piece_qty) pq 
from invoices_products group by season_id, product_id

create view viewOrders
as
select season_id, product_id, sum(piece_qty) pq 
from orders_products group by season_id, product_id

select pp.id, 
       pp.season_id, 
       pp.product_id, 
       pp.product_price, 
       i.pq as total_invoice, 
       o.pq as total_order
from products_price pp

left join viewInvoices as i 
           on pp.season_id = i.season_id and pp.product_id = i.product_id

left join viewOrders as o 
           on pp.season_id = o.season_id and pp.product_id = o.product_id

试试这个查询

SELECT * from products_price as a left join (select product_id, sum(piece_qty)total_invoices from invoices_products group by product_id) as b on a.product_id=b.product_id left join (select product_id, sum(piece_qty) as total_order from orders_products group by product_id) as c on a.product_id=c.product_id

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

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