【问题标题】:DQL associations and lat lon distanceDQL 关联和纬度距离
【发布时间】:2014-05-27 15:35:34
【问题描述】:

实体A 有一个字段name

实体B有一个字段city

实体C 具有字段latlon

实体A 与实体B 具有一对多关联,名为bees

实体B 与实体C 具有多对一关联,名为cees

现在我想选择距离 Y 点 X 公里范围内的所有 A 实体。

X = 20

Y = lat:52.25 , lon:4.53

DQL: SELECT a, ( 6371 * acos( cos( radians(52.25) ) * cos( radians( c.lat ) ) * cos( radians( c.lon ) - radians(4.53) ) + sin( radians(52.25) ) * sin( radians( c.lat ) ) ) ) AS distance FROM A a LEFT JOIN a.bees b LEFT JOIN b.cees c WHERE distance < 20

当我执行这个 DQL 时,我得到:

An exception occurred while executing 'SELECT m0_.name AS name0, m0_.id AS id11, (6371 * ACOS(COS(RADIANS(52.25)) * COS(RADIANS(p1_.lat)) * COS(RADIANS(p1_.lon) - RADIANS(4.53)) + SIN(RADIANS(52.25)) * SIN(RADIANS(p1_.lat)))) AS sclr12, FROM table_a m0_ LEFT JOIN table_b m2_ ON m0_.id = m2_.a_id LEFT JOIN table_c p1_ ON m2_.b_id = p1_.id WHERE sclr12 < 20':

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr12' in 'where clause'`

我在这里做错了什么? 非常感谢任何帮助!

约拉姆

【问题讨论】:

  • 在你的原始语句中你说distance &lt; 20,在你的代码中你说sclr12 &lt; 20。您没有名为 sclr12 的列
  • 没错,但问题是当我执行 DQL 时,它会将其更改为您在错误中看到的内容。正如您在错误中的 SQL 中看到的那样,而不是将距离计算命名为 distance 现在命名为 sclr12 但它仍然说列 sclr12 不存在.. SELECT ... (6371 * ACOS( ... )) AS sclr12

标签: php mysql doctrine-orm zend-framework2 dql


【解决方案1】:

您不能在 where 子句中引用别名。

您可能会在这里找到解决方案:SQL Use alias in Where statement

【讨论】:

  • 谢谢,不知道!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 2011-10-01
  • 1970-01-01
相关资源
最近更新 更多