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

时间:2015-04-26 06:09:19

标签: mysql sql

我有3张桌子。产品价格,发票产品和订购产品的表格。我正在尝试创建一个连接这些的视图。我想输出产品价格,共计发票产品和订购产品。

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
....

以下是我迄今为止尝试的View语句中的一些查询。

这个查询给了我想要的一切。 View的输出是完美的,但前两行的SUM()是关闭的。对于第1行和第2行,total_invoice_product为双倍。total_order_product对于第1行为4x,对于第2行为3x。

声明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`

此查询为我提供了我期望的输出。它不是我想要的完整输出,但它对于声明是正确的。 SUM()总数也是关于这个。

声明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`

我想要的输出

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 

收到声明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 

收到声明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

我可以像下面这样构建一个查询,它完美无缺。我得到了我需要的确切输出,但是这段代码不能用作视图。我收到此错误:错误1349:查看的SELECT包含FROM子句SQL语句中的子查询

完美查询,但不会作为视图

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

我需要什么

我已经尝试过其他一些陈述。他们要么得到更糟糕的结果,要么相同。有人可以帮助我使用适当的SUM来处理语句1吗?

为问题编辑1

此视图提供的信息将被大量调用。 products_price和invcoices_products表不会经常更改。 orders_products将会发生很大变化。如果需要2个视图,那么使用" Perfect"会更有效吗?查询上面或使用2个视图?

编辑2以进行其他查询

这是我的视图声明中的另一个查询。此查询是上面显示的声明1 的一部分。此查询工作正常但不完整。我需要第二个SUM列。当你添加第二个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`

输出

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 

2 个答案:

答案 0 :(得分:0)

嗯,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

答案 1 :(得分:0)

尝试此查询

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