更新时间:2023-10-23 14:19:22
您不能在 where
子句中重复使用 select
别名.您需要重复表达式,或者使用子查询或 cte.
You can't reuse a select
alias in the where
clause. You need to repeat the expression, or use a subquery or cte.
就其价值而言,您可以使用 between
使表达式在 where
子句中只出现一次而不是两次:
For what it's worth, you can use between
to have the expression just once instead of twice in the where
clause:
SELECT date_part('year', CURRENT_DATE) - f.birth_year AS age
FROM public.foo f
WHERE date_part('year', CURRENT_DATE) - f.birth_year BETWEEN 20 AND 30
至于子查询解决方案,那就是:
As for the subquery solution, that would be:
SELECT *
FROM (
SELECT date_part('year', CURRENT_DATE) - f.birth_year AS age
FROM public.foo
) f
WHERE age BETWEEN 20 AND 30