select distinct hash, title, count(*) as c
from pastes
where hash is not null
group by hash, title
order by c desc;
我可以根据我在查询中即时定义的“c”列对此查询的结果进行排序。
但我还想在WHERE子句中添加c:
select distinct hash, title, count(*) as c
from pastes
where c > 10 and hash is not null
group by hash, title
order by c desc;
ERROR: column "c" does not exist
LINE 1: ...inct hash, title, count(*) as c from pastes where c > 10 and...
^
指定这样的查询的正确方法是什么?
答案 0 :(得分:2)
使用having
select hash, title, count(*) as c
from pastes
where hash is not null
group by hash, title
having count(*) > 10
order by c desc;
答案 1 :(得分:0)
您可以将其包装为子查询:
select q.* from (
select hash, title, count(*) as c
from pastes where hash is not null
group by hash, title
) q
where q.c > 10
order by q.c desc;
您可以跳过子查询并使用HAVING而不是WHERE,但我发现您还需要ORDER BY c,因此子查询。