像mysql count frequency这样的帖子在获取mysql表中字段的频率列表时非常有用。如果我想设置一个阈值,这样只有一些符合标准的频率被“选择”或显示出来。如。
SELECT age, COUNT(*) AS freq FROM ages GROUP BY age WHERE COUNT(*) > 20
但是上面的命令得到了一个错误。什么是正确的命令?
qkf9rpyu1#
将where子句移到组之前,方法是:
SELECT age, COUNT(*) AS freq FROM ages WHERE COUNT(*) > 20 GROUP BY age
l0oc07j22#
你可能想用 HAVING 条款如下:
HAVING
SELECT age, COUNT(*) AS freq FROM ages GROUP BY age HAVING COUNT(*) > 20; ``` `HAVING` 扮演类似的角色 `WHERE` ,但前者作用于记录的组或集合,后者作用于单个记录。由于要限制记录组的行为,在执行 `GROUP BY` ,您要使用 `HAVING` .
cbwuti443#
必须使用having,但必须对新字段“freq”进行过滤:
SELECT age, COUNT(*) AS freq FROM ages GROUP BY age HAVING freq > 20;
3条答案
按热度按时间qkf9rpyu1#
将where子句移到组之前,方法是:
l0oc07j22#
你可能想用
HAVING
条款如下:cbwuti443#
必须使用having,但必须对新字段“freq”进行过滤: