1

我有一个查询,用于汇总库存总额并显示每个库存项目的最后更新日期。我有两种不同的库存类型 0 和 1(传入、传出)。我希望能够在页面底部放置“最后收到的库存是:”和“最后一次发货是:”。

这是我的主要查询对库存进行分组和汇总的内容:

 $query = "Select * 
          FROM (SELECT id, type, color, product, 
                      SUM(Quantity) AS TotalQuantity, MAX(Date) AS LatestDate 
                FROM inventory 
                GROUP BY id, color, type) AS alias 
          WHERE TotalQuantity > 0"; 

现在我还希望能够运行这两个查询来获取“库存”0 和 1 的最后更新时间(传入、传出)

$query = "SELECT MAX(Date) FROM inventory WHERE stock = 0";

$query = "SELECT MAX(Date) FROM inventory WHERE stock = 1";

结合这三个查询有什么帮助吗?我正在尝试 UNION 但运气不佳。

4

5 回答 5

3

尝试这样的事情:

SELECT  alias.*,
    zeroStock.zeroDate,
    nonzeroStock.nonzeroDate
FROM
    (
        SELECT  id, 
            type, 
            color, 
            product, 
            SUM(Quantity) AS TotalQuantity, 
            MAX(Date) AS LatestDate 
        FROM    inventory 
        GROUP BY id, color, type
    ) alias INNER JOIN
    (
        SELECT id, MAX(Date) zeroDate
        FROM inventory 
        WHERE stock = 0
        GROUP BY id
    ) zeroStock on alias.id = zeroStock.id
    INNER JOIN
    (
        SELECT id, MAX(Date) nonzeroDate
        FROM inventory 
        WHERE stock = 1
        GROUP BY id
    ) nonzeroStock on alias.id = nonzeroStock.id
WHERE alias.TotalQuantity > 0
于 2012-06-21T05:43:50.730 回答
2

Try this one

Select
  alias.id,
  alias.type,
  alias.color,
  alias.product,
  Stock0.S0,
  Stock1.S1
FROM (SELECT
        id,
        type,
        color,
        product,
        SUM(Quantity)  AS TotalQuantity,
        MAX(Date)      AS LatestDate
      FROM inventory
      GROUP BY id, color, type) AS alias
  left join (SELECT
               id,
               MAX(Date)         S0
             FROM inventory
             WHERE stock = 0) as Stock0
    on Stock0.id = alias.id
  left join (SELECT
               id,
               MAX(Date)         S1
             FROM inventory
             WHERE stock = 0) as Stock1
    on Stock1.id = alias.id
WHERE TotalQuantity > 0
于 2012-06-21T06:42:18.407 回答
1

您可以使用 aCROSS JOIN将日期添加到常规选择语句的每一行。

SELECT  *
FROM    (
          SELECT  *
          FROM    (
                    SELECT id, type, color, product, SUM(Quantity) AS TotalQuantity, MAX(Date) AS LatestDate 
                    FROM inventory 
                    GROUP BY 
                          id, color, type
                  ) AS alias 
          WHERE TotalQuantity > 0
        ) AS q
        CROSS JOIN (SELECT MAX(Date) AS MaxIncoming FROM Inventory WHERE stock = 0) AS r
        CROSS JOIN (SELECT MAX(Date) AS MaxOutgoing FROM Inventory WHERE stock = 1) AS s

Mysql 交叉连接

交叉联接也称为笛卡尔积联接。SQL 中的交叉连接返回一个结果表,其中第一个表中的每一行与第二个表中的每一行相结合

编辑

作为记录,您可以通过使用HAVING子句而不是WHERETotalQuantity 上的子句来简化语句。

SELECT  *
FROM    (
          SELECT  id, type, color, product, SUM(Quantity) AS TotalQuantity, MAX(Date) AS LatestDate 
          FROM    inventory 
          GROUP BY 
                  id, color, type
          HAVING
                  SUM(Quantity) > 0        
        ) AS q
        CROSS JOIN (SELECT MAX(Date) AS MaxIncoming FROM Inventory WHERE stock = 0) AS r
        CROSS JOIN (SELECT MAX(Date) AS MaxOutgoing FROM Inventory WHERE stock = 1) AS s        
于 2012-06-21T05:45:29.653 回答
0

最好将查询 2 和 3 与查询 1 分开运行。查询 2 和 3 与查询 1 不同,因为您打算获取单个值而不是多列列表。此外,您正在过滤查询 1 中的结果,而不是 2 和 3 中的结果。

当然,您可以CROSS JOIN在子查询上进行两次,但是您将在数据库和应用程序之间传输更多不必要的数据。您只需要检索两个,而不是两个完整的列(我可能会添加,它与结果集的其余部分无关——进一步强调了它的不优雅)。

此外,该CROSS JOIN方法假设至少有一行满足 的条件SUM(Quantity) > 0。如果他们都不满意怎么办?...那么您也不会有最新的进/出库存日期!尽管可能不太可能,但必须考虑此用例。

话虽如此,您可以重写您的第一个查询以避免子选择:

SELECT 
    id,
    color,
    type,
    product,
    SUM(Quantity) AS TotalQuantity,
    MAX(Date) AS LatestDate
FROM 
    inventory 
GROUP BY 
    id, 
    color, 
    type
HAVING
    SUM(Quantity) > 0

您可以使用聚合函数中的条件检查来组合查询 2 和 3 MAX()

SELECT
    MAX(CASE WHEN stock = 0 THEN Date ELSE NULL END) AS s0_maxdate,
    MAX(CASE WHEN stock = 1 THEN Date ELSE NULL END) AS s1_maxdate
FROM
    inventory
于 2012-06-21T07:17:08.677 回答
0
SELECT id, type, color, product, 
                  SUM(Quantity) AS TotalQuantity, MAX(Date) AS LatestDate, null AS stock 
            FROM inventory 
            GROUP BY id, color, type
HAVING TotalQuantity > 0

UNION ALL 

SELECT null, null, null, null, null, MAX(Date), stock 
FROM inventory WHERE stock IN (0,1) GROUP BY stock
于 2012-06-21T06:55:30.743 回答