“名称”列不存在

问题描述 投票:0回答:1

我在 PSQL 查询中遇到了 ALIAS 变量“distance”的问题。 (*请忽略我使用票数作为距离)

SELECT rentalid, createdDate, votecount AS distance
FROM rental 
WHERE longitude=? AND latitude=?   
HAVING distance < 25 ORDER BY distance LIMIT 0 OFFSET 30

错误是“距离不存在”,但我已经定义了距离,所以我无法判断问题是什么。

nested exception is org.postgresql.util.PSQLException: ERROR: column "distance" does not exist
  Position: 107] with root cause
postgresql psql jdbctemplate
1个回答
0
投票

尝试这种方法,首先创建一个计算距离别名的子查询,然后在外部查询中对结果进行过滤和排序。

SELECT *
FROM (
    SELECT rentalid, createdDate, votecount AS distance
    FROM rental 
    WHERE longitude = ? AND latitude = ?
) AS subquery
WHERE distance < 25
ORDER BY distance
LIMIT 30 OFFSET 0;

希望它有效:)

© www.soinside.com 2019 - 2024. All rights reserved.