MySQL选择语句创建列然后比较

时间:2018-06-06 12:47:21

标签: mysql sql

非常新的SQL。尝试仅在TotalSales小于目标时才显示结果。

SELECT SUM(Table1.Column1) AS TotalSales
FROM Table 1
WHERE Table1.Goal > TotalSales
GROUP BY EmployeeID;

它给了我错误1054:未知列。

同样,非常基本,但我被卡住了。再次感谢您的帮助。

1 个答案:

答案 0 :(得分:-1)

subquery需要JOIN

SELECT t1.*, t2.TotalSales
FROM TABLE1 t1 INNER JOIN (
       SELECT EmployeeID, SUM(Table1.Column1) AS TotalSales
       FROM Table1
       GROUP BY EmployeeID ) t2
ON t1.EmployeeID = t2.EmployeeID 
WHERE t1.Goal > t2.TotalSales;