liaosnet 发表于 2008-02-28 13:38

原帖由 oicq63236 于 2008-2-28 11:34 发表 http://bbs.chinaunix.net/images/common/back.gif
:wink: 怎么把COUNT(*)出的数,当条件使用呢?例如我想查大于COUNT(*)出10的记录?

select a, count(*) as b
from table_name
where b>"10"
group by 1;

ivhb 发表于 2008-02-28 14:36

原帖由 liaosnet 于 2008-2-28 13:38 发表 http://bbs.chinaunix.net/images/common/back.gif


select a, count(*) as b
from table_name
where b>"10"
group by 1;

这个真的可以么?
需要having子句吧

select
   a, count (*)
from table
group by a
having count (*) > 10
;

oicq63236 发表于 2008-02-28 14:44

原帖由 ivhb 于 2008-2-28 14:36 发表 http://bbs.chinaunix.net/images/common/back.gif


这个真的可以么?
需要having子句吧

select
   a, count (*)
from table
group by a
having count (*) > 10
;
确实得HAVING,没有的话行不通,用HAVING也就得使用count(*) ,而不能使用 count(*) as b....,having b > 10,这样提示

217: Column (b) not found in any table in the query (or SLV is undefined).
Error in line 1
Near character position 57

liaosnet 发表于 2008-02-28 15:09

回复 #12 ivhb 的帖子

:mrgreen: 的确得用having
页: 1 [2]
查看完整版本: 如何按出现次数多少排序查询固定表?