我有一个MySQL请求:
SELECT U.*, SUM(P.cost) AS spent FROM `#__users` AS U LEFT JOIN `#__projects` AS P ON P.client_id = U.id WHERE 1=1 AND (spent >= 1000 AND spent < 2000) GROUP BY U.id ORDER BY U.user_creation_timestamp DESC LIMIT 25 OFFSET 0;
但是我在这里有错误:
Column not found: 1054 Unknown column 'spent' in 'where clause'
我该如何解决该问题?谢谢!
您不能在哪里使用别名,也不能在必须使用过滤器聚合功能的位置使用过滤器聚合功能。
SELECT
U.*,
SUM(P.cost) AS spent
FROM `#__users` AS U
LEFT JOIN `#__projects` AS P ON P.client_id = U.id
GROUP BY U.id
HAVING (SUM(P.cost) >= 1000 AND SUM(P.cost) < 2000)
ORDER BY U.user_creation_timestamp DESC LIMIT 25 OFFSET 0;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句