0
當我執行以下命令它告訴我從一組MySQL的統計記錄
#1247 - 參考 'StockCount' 不支持(參照組功能)
SELECT stockSymbol, count(*) as StockCount
FROM US500
group by stockSymbol
AND StockCount > 100
ORDER BY stockSymbol DESC
打算什麼爲什麼它不工作。
當我執行以下命令它告訴我從一組MySQL的統計記錄
#1247 - 參考 'StockCount' 不支持(參照組功能)
SELECT stockSymbol, count(*) as StockCount
FROM US500
group by stockSymbol
AND StockCount > 100
ORDER BY stockSymbol DESC
打算什麼爲什麼它不工作。
SELECT
和任何WHERE
子句在任何聚合GROUP BY
函數之前應用。
如果你嘗試這個,你會得到關於未知列StockCount
錯誤的WHERE
子句中:
SELECT stockSymbol, count(*) as StockCount
FROM US500
WHERE StockCount > 100
GROUP BY stockSymbol
ORDER BY stockSymbol DESC
相反,使用HAVING
發生後SELECT
,WHERE
,並GROUP BY
適用於從彙總結果的條件GROUP BY
:
SELECT stockSymbol, count(*) as StockCount
FROM US500
/* WHERE clause would go here */
GROUP BY stockSymbol
HAVING StockCount > 100
ORDER BY stockSymbol DESC
ORDER BY
最後發生,所以你也可以參考聚合列那裏,如果喲你想:
SELECT stockSymbol, count(*) as StockCount
FROM US500
GROUP BY stockSymbol
HAVING StockCount > 100
ORDER BY StockCount DESC
您的查詢中的子句的順序應該給你一些他們處理的順序的想法。例如。 GROUP BY
子句必須在WHERE
子句和HAVING
必須在GROUP BY
之後。
聚合條件需要通過'having'來完成,而不是在where子句中。 –
用'HAVING'替換''和' – AgRizzo
謝謝你做的伎倆! – XK8ER