结合三个(复杂)查询

时间:2012-06-21 05:17:16

标签: mysql

我有一个查询,我用它来计算库存总数并显示每个库存项目的最新更新日期。我有两种不同的股票类型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"; 

现在我希望能够运行这两个查询以获取'stock'0和1(Incoming,Outgoing)的最后更新时间

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

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

结合这三个查询的任何帮助?我正在尝试UNION,但没有太多运气。

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

试试这个

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

答案 2 :(得分:1)

您可以使用CROSS 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 Cross Join

  

Cross Join也称为Cartesian Product Join。十字架加入   SQL返回一个结果表,其中第一个表中的每一行   与第二个表中的每一行组合

修改

对于记录,您可以使用HAVING子句而不是TotalQuantity上的WHERE子句来简化语句。

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        

答案 3 :(得分: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

答案 4 :(得分:0)

最好与查询1分开运行查询2和3.查询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

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

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