这件事很好:
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.