DQL关联和lat lon距离

时间:2014-05-27 15:35:34

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

实体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'`

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

约拉姆

1 个答案:

答案 0 :(得分:1)

您无法在where子句中引用别名。

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