SUM项目&具有多个表格的数量&如果MySQL

时间:2016-07-26 15:11:20

标签: mysql

我想要显示所有带数量的项目。但数量在不同的表格中

示例输出:

Item    Quantity
Item1   34
Item2   10
Item3   0
........

我的代码可以计算数量,但只能在单件商品中。

SELECT SUM(ROUND(Quantity)) FROM(
SELECT
COALESCE(SUM(od.Quantity),0) as 'Quantity'
FROM sts as T1
LEFT JOIN orderset_details as od ON T1.OrdersetId = od.OrdersetId
WHERE od.ItemId = '4786' 
UNION ALL
SELECT 
COALESCE(SUM(od.Quantity),0) as 'Quantity'
FROM drs as T2
LEFT JOIN orderset_details as od ON T2.OrdersetId = od.OrdersetId
WHERE od.ItemId = '4786' 
UNION ALL  
SELECT 
COALESCE(SUM(IF(r.AddsToStock = '1', od.Quantity, 0 - od.Quantity)),0) as 'Quantity'
FROM stockadjustment as T3
LEFT JOIN reason as r ON r.ReasonId = T3.ReasonId
LEFT JOIN orderset_details as od ON T3.OrdersetId = od.OrdersetId 
WHERE od.ItemId = '4786'
UNION ALL
SELECT 
COALESCE(SUM(IF(T4.OrdersetId = od.OrdersetId, 0 - od.Quantity, -1 * od.Quantity)),0) as 'Quantity'
FROM issueslip as T4
LEFT JOIN orderset_details as od ON T4.OrdersetId = od.OrdersetId 
WHERE od.ItemId = '4786'
UNION ALL
SELECT 
COALESCE(SUM(IF(T4.OrdersetId = od.OrdersetId, 0 - od.Quantity, -1 * od.Quantity)),0) as 'Quantity'
FROM invoice as T4
LEFT JOIN orderset_details as od ON T4.OrdersetId = od.OrdersetId
WHERE od.ItemId = '4786') as t

我如何使用此代码实现这些项目?

表格名称 items,ID为ItemId

1 个答案:

答案 0 :(得分:1)

  1. 创建此存储函数:

    DELIMITER //
    
    CREATE FUNCTION getItemQty(
        _itemID INT(10)
    )
    RETURNS INT
    BEGIN
    
    RETURN (SELECT 
            SUM(ROUND(Quantity)) 
        FROM (
            SELECT
                COALESCE(SUM(od.Quantity),0) as Quantity
            FROM sts as T1
            LEFT JOIN orderset_details as od 
                ON T1.OrdersetId = od.OrdersetId
            WHERE od.ItemId = _itemID
            UNION ALL
            SELECT 
                COALESCE(SUM(od.Quantity),0) as Quantity
            FROM drs as T2
            LEFT JOIN orderset_details as od 
                ON T2.OrdersetId = od.OrdersetId
            WHERE od.ItemId = _itemID
            UNION ALL  
            SELECT 
                COALESCE(
                    SUM(
                        IF(r.AddsToStock = '1', od.Quantity, 0 - od.Quantity)
                    ),0
                ) as Quantity
            FROM stockadjustment as T3
            LEFT JOIN reason as r 
                ON r.ReasonId = T3.ReasonId
            LEFT JOIN orderset_details as od 
                ON T3.OrdersetId = od.OrdersetId 
            WHERE od.ItemId = _itemID
            UNION ALL
            SELECT 
                COALESCE(
                    SUM(
                        IF(T4.OrdersetId = od.OrdersetId, 0 - od.Quantity, -1 * od.Quantity)
                    ),0
                ) as Quantity
            FROM issueslip as T4
            LEFT JOIN orderset_details as od 
                ON T4.OrdersetId = od.OrdersetId 
            WHERE od.ItemId = _itemID
            UNION ALL
            SELECT 
                COALESCE(
                    SUM(
                        IF(T4.OrdersetId = od.OrdersetId, 0 - od.Quantity, -1 * od.Quantity)
                    ),0
                ) as Quantity
            FROM invoice as T4
            LEFT JOIN orderset_details as od 
                ON T4.OrdersetId = od.OrdersetId
            WHERE od.ItemId = _itemID
        ) as t);
    END//
    
  2. 使用该查询获取数据。

    SELECT 
        ItemId,
        getItemQty(ItemId) AS quantity
    FROM items