0

我尝试使用这个 HQL 查询:

   Result.find("SELECT c, ( 3959 * acos( cos( radians(?) ) * "+
    "cos( radians( c.latitude ) ) *"+
    "cos( radians( c.longitude ) - radians(?) ) +"+
    "sin( radians(?) ) * sin( radians( c.latitude ) ) ) ) " +
    "AS distance FROM City c HAVING distance < ? ORDER BY distance ASC",
    latitude, longitude, latitude, radius).fetch();

但结果是:

IllegalArgumentException occured : org.hibernate.hql.ast.QuerySyntaxException: unexpected token: HAVING near line 1, column 204 [SELECT c, ( 3959 * acos( cos( radians(?) ) * cos( radians( c.latitude ) ) *cos( radians( c.longitude ) - radians(?) ) +sin( radians(?) ) * sin( radians( c.latitude ) ) ) ) AS distance FROM models.City c HAVING distance < ? ORDER BY distance ASC]
4

5 回答 5

1

尝试在您的查询中更改HAVING为, 。WHERE

于 2012-04-12T06:29:09.813 回答
1

仅当您使用 GROUP BY 时才允许使用关键字 HAVING(考虑使用 WHERE 表示 GROUPS)。请参阅 SELECT 语法的参考手册

于 2012-04-12T06:29:41.263 回答
1

唯一的方法是使用本机查询,因为:

Java代码:

@Query(value = "SELECT s.*,\n" +
            "  (\n" +
            "    3959 * acos(\n" +
            "        cos(radians(s.lat))\n" +
            "        * cos(radians(:lat))\n" +
            "        * cos(radians(:lon) - radians(s.lon))\n" +
            "        + sin(radians(s.lat))\n" +
            "          * sin(radians(:lat))\n" +
            "    )\n" +
            "  ) AS distance\n" +
            "FROM Stop s\n" +
            "HAVING distance < 30\n" +
            "ORDER BY distance asc", nativeQuery = true)
    List<Stop> findClosestStops(@Param("lat") Double lat, @Param("lon") Double lon);

另一种方法是返回包含城市+距离的对象列表,但在哪里不可能,但您可以限制结果集

于 2015-11-10T20:15:04.660 回答
0

尝试

 select dis.c,dis.distance(
    SELECT c,
     ( 3959 * acos( cos( radians(?) ) *  cos( radians( c.latitude ) ) * 
cos( radians( c.longitude ) - radians(?) ) + sin( radians(?) ) *
 sin( radians( c.latitude ) ) ) )  AS distance FROM City c  ) dis 
where dis.distance<? order by dis.distance ASC
于 2012-04-12T14:14:43.803 回答
0

嗯,应该是什么“?” 范围?

如果为真,您应该像这样使用它

HAVING distance < :distance

并使用

query.setParameter("distance", 50);
于 2012-04-12T06:31:20.030 回答