我有以下SQL查詢:SQL子查詢:行中找不到
SELECT t1.`userID`,`name`, COUNT(*) AS `count`, (SELECT `val` FROM `user-data` WHERE `userID` = t1.userID AND `keyID` = 2 LIMIT 1) AS `staff`
FROM `activity` t1
LEFT JOIN `users` ON `users`.`id` = t1.`userID`
WHERE
t1.`userID` <> 0
GROUP BY t1.`userID`
ORDER BY `count` DESC LIMIT 10
其中一期工程,它需要大約2秒。但是,我想找個地方staff <> 1
條目:
SELECT t1.`userID`,`name`, COUNT(*) AS `count`, (SELECT `val` FROM `user-data` WHERE `userID` = t1.userID AND `keyID` = 2 LIMIT 1) AS `staff`
FROM `activity` t1
LEFT JOIN `users` ON `users`.`id` = t1.`userID`
WHERE
t1.`userID` <> 0
AND `staff` <> 1
GROUP BY t1.`userID`
ORDER BY `count` DESC LIMIT 10
我得到的錯誤:Unknown column 'staff' in 'where clause'
我可以把子查詢的WHERE子句中,但隨後的查詢需要70秒。
但我當然應該可以訪問我在SELECT語句中引用的列嗎?
'和staff' <'是不完整的 –
只是檢查,你忘了在你的查詢中添加AND'staff' <> 1? – lloiacono
不,你不應該。選擇列表在** where子句後處理**。 – Shadow