在Doctrine和pgadmin中不存在具有子句的列问题

时间:2016-05-20 06:51:56

标签: postgresql doctrine-orm pgadmin

我在pgadmin中运行查询但面临问题列距离不存在

select f.title, f.longitude, f.latitude, (3959 * cos(cos(radians('52.512452')) * cos(radians(latitude)) * cos(radians(longitude) - radians('13.390931')) + sin(radians('52.512452')) * sin(radians(latitude)))) AS distance from fitness_studio f having distance<1 order by distance desc

提前感谢您的帮助。

此致

阿伊莎

1 个答案:

答案 0 :(得分:2)

据我所知,postgresql无法直接在where子句中使用别名列。所以你应该尝试复制逻辑:

SELECT
  f.title,
  f.longitude,
  f.latitude,
  (3959 * cos(cos(radians('52.512452')) * cos(radians(latitude)) * cos(radians(longitude) 
- radians('13.390931')) + sin(radians('52.512452')) * sin(radians(latitude)))) AS distance
  FROM fitness_studio f
 WHERE (3959 * cos(cos(radians('52.512452')) * cos(radians(latitude)) *
        cos(radians(longitude) - radians('13.390931')) + 
        sin(radians('52.512452')) * sin(radians(latitude)))) < 1
 ORDER BY distance DESC

要么使用子查询:

WITH container AS (
SELECT
  f.title,
  f.longitude,
  f.latitude,
  (3959 * cos(cos(radians('52.512452')) * cos(radians(latitude)) *
   cos(radians(longitude) - radians('13.390931')) +
   sin(radians('52.512452')) * sin(radians(latitude)))) AS distance
FROM fitness_studio f)
SELECT *
  FROM container
 WHERE distance < 1
 ORDER BY distance DESC

请记住,使用此类子查询可能会对执行计划产生负面影响,当您的表足够大时,执行速度变得比查询笨拙更重要。

PS:请注意,ORDER BY可以正确获取别名作为参数。假设它的原因ORDER BY不影响选定的行,它只是旋转它们。与GROUP BY相同的图片