在mysql中计算和多个连接

时间:2012-06-22 07:30:24

标签: mysql sql

1.products(产品表)

1.productid 

  001         
  002         
  003         

2.productname

  x
  y
  z

2.invoiceqty(InvoiceQuantity Table)

1.productid

  001       
  001      
  002       
  002 

2.invqty

  10
  20
  10
  05

3.grnqty(GRN数量表)

1.productid 

  001        
  002        
  003        

2.grqty

  50
  50
  50

4.returninqty(在数量表中返回)

1.productid 

  002
  002

2.retinqty

  02
  03

5.returnoutqty(退货数量表)

1.productid 

  001
  001

2.retoutqty

  01
  01

我需要执行此查询才能查看库存余额 像这样的库存平衡计算

(grnqty + returninqty) - (invoiceqty + returnoutqty)= StockBalance

请告诉我如何编写查询来计算这样的库存余额 我已经在java / MySQL中开发了库存控制系统,请帮我解决这个问题

6.Stocks(QUERY)

1.productid 

  001
  002
  003

2.Stockqty

  40
  33
  50

2 个答案:

答案 0 :(得分:1)

尝试使用子查询计算每个表的数量。它不适用于LEFT JOIN,因为我们在表格中会有一个笛卡尔积。

SELECT
    p.productid,
(
    SELECT COALESCE(SUM(grnqty), 0) 
    FROM grnqty g 
    WHERE g.productid = p.productid
) 
 +
(
    SELECT COALESCE(SUM(returninqty), 0) 
    FROM returninqty ri 
    WHERE ri.productid = p.productid
)     
 - 
(
    SELECT COALESCE(SUM(invoiceqty), 0) 
    FROM invoiceqty i 
    WHERE i.productid = p.productid
)
 -
(
    SELECT COALESCE(SUM(returnoutqty), 0) 
    FROM returnoutqty ro 
    WHERE ro.productid = p.productid
) as stock

FROM products p

此外,您可以创建一个计算股票的功能。您可以轻松地将其重复用于其他查询。

DELIMITER $$

CREATE FUNCTION `get_product_stock` (IN p_id_product INT) RETURNS INT
BEGIN

DECLARE stock INT;

SET stock =
    (
        SELECT COALESCE(SUM(grnqty), 0)
        FROM grnqty
        WHERE productid = p_id_product
    )
     +
    (
        SELECT COALESCE(SUM(returninqty), 0)
        FROM returninqty
        WHERE productid = p_id_product
    )
     -
    (
        SELECT COALESCE(SUM(invoiceqty), 0)
        FROM invoiceqty
        WHERE productid = p_id_product
    )
     -
    (
        SELECT COALESCE(SUM(returnoutqty), 0)
        FROM returnoutqty
        WHERE productid = p_id_product
    );

RETURN stock;

END $$

DELIMITER ;

您可以使用以下功能:

// get the stock for product with id 1
SELECT get_product_stock(1); 

// get all the products with their stock
SELECT productid, name, get_product_stock(productid) as stock FROM products; 

答案 1 :(得分:0)

此查询是根据您的公式但是您显示的结果不会生成,因为我认为您的公式可能有任何问题

SELECT
  p.productid,

  (ifnull(g.g_sum,0) + ifnull(rq.rq_sum,0))-(ifnull(i.i_sum,0) + ifnull(ro.ro_sum,0)) as StockBalance,

  ifnull(g.g_sum,0) as GRN_QUANTITY,
 ifnull( rq.rq_sum,0) as RETURN_IN_QUANTITY,
 ifnull( i.i_sum,0) as INVENTORY_QUANTITY,
 ifnull( ro.ro_sum,0) as RETURN_OUT_QUANTITY
from products as p
  left join (select
           productid,
           sum(grnqty)  as g_sum
         from grnqty group by productid) as g
    on g.productid = p.productid
  left join (select
           productid,
           sum(retinqty) as rq_sum
         from returninqty group by productid) as rq
    on rq.productid = p.productid
  left join (select
           productid,
           sum(invqty)  as i_sum
         from invoiceqty group by productid) as i
    on i.productid = p.productid
  left join (select
           productid,
           sum(retoutqty) as ro_sum
         from returnoutqty group by productid) as ro
    on ro.productid = p.productid