我想做这样的事情:
select
case when (select count(*) as score from users t1 ) >5 THEN score else 0 end
当我尝试它时,我得到错误:
column score doesn't exists.
我能以其他方式做到这一点吗?我需要它来设置LIMIT值。我当然希望这样做:
select
case when (select count(*) as score from users t1 ) >5 THEN (select count(*) as score from users) else 0 end
但是我需要执行两次相同的查询。 有人一些想法吗?
答案 0 :(得分:4)
您可以使用WITH
子句:
with a as (select count(*) score from t)
select case when score > 5 then score else 0 end from a;
或子查询(内联视图):
select case when score > 5 then score else 0 end
from (select count(*) score from t) t;