有什么方法可以在查询中引用MySQL“AS”值吗?

时间:2013-01-22 10:58:02

标签: mysql sql join

在以下查询中,我总结了给定范围内所有销售的所有销售项目。本摘要中包括“sumOfCost”和“sumOfPrice”,我也喜欢“sumOfProfit”。问题是,因为它们只是“AS”变量,我不确定如何引用它们,而我的尝试“...... Blah AS blah,sumOfPrice - sumOfCost = sumOfProfit ......”遗憾地不起作用!

这是我的问题:

SELECT  Sales.SaleID, 
        Sales.StaffID, 
        Sales.CustomerID, 
        Sales.Timestamp, 
        Sales.Refunded, 
        Sales.PaymentType, 
        Staff.Forename AS staffForename, 
        Staff.Surname AS staffSurname, 
        (   
            SELECT GROUP_CONCAT(Quantity, ' x ', Name) 
            FROM SaleItems 
            WHERE SaleItems.SaleID = Sales.SaleID
        ) AS itemList, 
        (
            SELECT sum(Cost*Quantity) 
            FROM SaleItems 
            WHERE SaleItems.SaleID = Sales.SaleID
        ) AS sumOfCost, 
        (
            SELECT sum(Price*Quantity) 
            FROM SaleItems 
            WHERE SaleItems.SaleID = Sales.SaleID
        ) AS sumOfPrice 
FROM    Sales 
        INNER JOIN Staff 
            ON Sales.StaffID = Staff.StaffID 
WHERE   Sales.Deleted = '0' 
ORDER   BY Timestamp DESC 
LIMIT   0, 15

对于查询的长度感到抱歉,我之前一直在努力优化它,但现在我只是想解决这个问题。

提前致谢:)

1 个答案:

答案 0 :(得分:2)

解决此问题的另一种方法是使用子查询包装整个查询,

SELECT  *,
        sumOfCost - sumOfPrice AS sumOfProfit
FROM
    (
        SELECT  Sales.SaleID, 
                Sales.StaffID, 
                Sales.CustomerID, 
                Sales.Timestamp, 
                Sales.Refunded, 
                Sales.PaymentType, 
                Staff.Forename AS staffForename, 
                Staff.Surname AS staffSurname, 
                (   
                    SELECT GROUP_CONCAT(Quantity, ' x ', Name) 
                    FROM SaleItems 
                    WHERE SaleItems.SaleID = Sales.SaleID
                ) AS itemList, 
                (
                    SELECT sum(Cost*Quantity) 
                    FROM SaleItems 
                    WHERE SaleItems.SaleID = Sales.SaleID
                ) AS sumOfCost, 
                (
                    SELECT sum(Price*Quantity) 
                    FROM SaleItems 
                    WHERE SaleItems.SaleID = Sales.SaleID
                ) AS sumOfPrice 
        FROM    Sales 
                INNER JOIN Staff 
                    ON Sales.StaffID = Staff.StaffID 
        WHERE   Sales.Deleted = '0' 
        ORDER   BY Timestamp DESC 
        LIMIT   0, 15
    ) s

顺便说一句,他们被称为ALIAS

ALIAS无法在定义它们的同一级别上进行计算的原因是服务器在FROM子句之前执行SELECT子句。 ALIAS子句位于SELECT子句中,这是完整的SQL操作顺序:

  • FROM clause
  • WHERE子句
  • GROUP BY子句
  • HAVING条款
  • SELECT条款
  • ORDER BY子句

为了更好的性能问题,我宁愿使用JOIN而不是subqueries

更新1

SELECT  Sales.SaleID, 
        Sales.StaffID, 
        Sales.CustomerID, 
        Sales.Timestamp, 
        Sales.Refunded, 
        Sales.PaymentType, 
        Staff.Forename AS staffForename, 
        Staff.Surname AS staffSurname, 
        COALESCE(a.itemList, '') itemList,
        COALESCE(b.sumOfCost, 0) sumOfCost, 
        COALESCE(c.sumOfPrice, 0) sumOfPrice,
        COALESCE(b.sumOfCost, 0) - COALESCE(c.sumOfPrice, 0) AS sumOfProfit
FROM    Sales 
        INNER JOIN Staff 
            ON Sales.StaffID = Staff.StaffID 
        LEFT JOIN 
        (   
            SELECT SaleID, GROUP_CONCAT(Quantity, ' x ', Name) itemList
            FROM SaleItems 
            GROUP BY SaleID
        ) a ON a.SaleID = Sales.SaleID
        LEFT JOIN
        (
            SELECT SaleID, sum(Cost*Quantity) sumOfCost
            FROM SaleItems 
            GROUP BY SaleID
        ) b ON b.SaleID = Sales.SaleID
        LEFT JOIN
        (
            SELECT SaleID, sum(Price*Quantity) sumOfPrice 
            FROM SaleItems 
            GROUP BY  SaleID
        ) c ON c.SaleID = Sales.SaleID
WHERE   Sales.Deleted = '0' 
ORDER   BY Timestamp DESC 
LIMIT   0, 15

更新2

SELECT  Sales.SaleID,
        Sales.StaffID,
        Sales.CustomerID,
        Sales.TIMESTAMP,
        Sales.Refunded,
        Sales.PaymentType,
        Staff.Forename AS staffForename,
        Staff.Surname AS staffSurname,
        COALESCE(a.itemList, '') itemList,
        COALESCE(a.sumOfCost, 0) sumOfCost,
        COALESCE(a.sumOfPrice, 0) sumOfPrice,
        COALESCE(a.sumOfCost, 0) - COALESCE(a.sumOfPrice, 0) AS sumOfProfit
FROM    Sales
        INNER JOIN Staff
            ON Sales.StaffID = Staff.StaffID
        LEFT JOIN
        (  
            SELECT      SaleID,
                        GROUP_CONCAT(Quantity, ' x ', Name) itemList,
                        SUM(Cost*Quantity) sumOfCost,
                        SUM(Price*Quantity) sumOfPrice
            FROM SaleItems
            GROUP BY SaleID
        ) a ON a.SaleID = Sales.SaleID
WHERE   Sales.Deleted = '0'
ORDER   BY TIMESTAMP DESC
LIMIT   0, 15