不能在MySQL表中计算更少/更大的值

时间:2015-09-02 19:03:57

标签: mysql sql

我正在尝试使用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 

enter image description here

你可以告诉我为什么会这样吗?以及如何解决这个问题?

2 个答案:

答案 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上使用索引(如果有的话)。