我正在尝试使用PHPMyAdmin在MYsQL表中COUNT行少于或大于100
SELECT HAVING COUNT(`roadLength`) > 100 AS stop3 FROM `single-ecolo-dis-yes-tbl`
和
SELECT HAVING COUNT(`roadLength`)< 100 FROM `single-ecolo-dis-yes-tbl`
但我收到此错误
SELECT HAVING COUNT(`roadLength`) > 100 FROM `single-ecolo-dis-yes-tbl`
LIMIT 0, 25
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'HAVING COUNT(`roadLength`) > 100 FROM `single-ecolo-dis-yes-tbl`
LIMIT 0, 25' at line 1
你可以告诉我为什么会这样吗?以及如何解决这个问题?
答案 0 :(得分:2)
having
是减少查询中的组,并且在select
子句中没有业务。它本身是clause
SELECT SUM(`roadLength` > 100) AS stop3
FROM `single-ecolo-dis-yes-tbl`
答案 1 :(得分:1)
通常,对于这样的简单计数,您可以将条件放在WHERE
子句中:
SELECT COUNT(*) as stop3
FROM `single-ecolo-dis-yes-tbl` t
WHERE t.roadLength > 100 ;
这允许优化器在roadLength
上使用索引(如果有的话)。