这件事很好:
SELECT c.id, c.name, c.ascii_name, COUNT(*) AS nr
FROM cities c
INNER JOIN jobs j ON (j.city_id = c.id )
WHERE j.is_active = 1
GROUP BY c.name
limit 100
但是,当我想把条件放在新列上时,它表示未找到列
SELECT c.id, c.name, c.ascii_name, COUNT(*) AS nr
FROM cities c
INNER JOIN jobs j ON (j.city_id = c.id )
WHERE j.is_active = 1 and nr > 100
GROUP BY c.name
limit 100
解决方法:
您应该在HAVING子句中将条件放在nr上,如下所示:
SELECT c.id, c.name, c.ascii_name, COUNT(*) AS nr
FROM cities c
INNER JOIN jobs j ON (j.city_id = c.id )
WHERE j.is_active = 1
GROUP BY c.name
HAVING nr > 100
limit 100
这是因为nr是聚合函数(COUNT(*))的结果,因此在应用WHERE过滤器时不可用.
编辑:在某些数据库服务器中,对nr的引用不起作用;你也可以使用HAVING COUNT(*)> 100.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。