Ser*_*.by 7 postgresql select sql-function where-clause
我试图在where子句中使用函数执行的结果,但没有成功:
SELECT clinics.*, distance_between_objects(1, id, 7, 3) AS dist FROM clinics WHERE dist<=1;
Run Code Online (Sandbox Code Playgroud)
给我:Column "dist" does not exists。引用它就像:
SELECT clinics.*, distance_between_objects(1, id, 7, 3) AS dist FROM clinics WHERE "dist"<=1;
Run Code Online (Sandbox Code Playgroud)
也没有帮助。请告知Postgres是否有可能在WHERE子句中使用函数结果而不调用它两次?谢谢!
为避免调用distance_between_objects两次:
--Subquery
SELECT * FROM (
SELECT
*,
distance_between_objects(1, id, 7, 3) AS dist
FROM
clinics) AS clinics_dist
WHERE
dist <= 1;
--CTE
WITH clinics_dist AS (
SELECT
*,
distance_between_objects(1, id, 7, 3) AS dist
FROM
clinics
)
SELECT
*
FROM
clinics_dist
WHERE
dist <= 1;
Run Code Online (Sandbox Code Playgroud)
CTE我认为这是一种更干净的方法。
| 归档时间: |
|
| 查看次数: |
8101 次 |
| 最近记录: |