gpt4 book ai didi

spring-data-jpa - JPQL @Query 导致 "unexpected token: ("

转载 作者:行者123 更新时间:2023-12-05 01:20:00 25 4
gpt4 key购买 nike

我定义了以下方法:

@Query("SELECT AVG(total) FROM (SELECT COUNT(t.name) total FROM DataTable t GROUP BY DATE(actiontime)) result")
Long countAvg();

但是它会导致这个错误:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected     token: ( near line 1, column 24 [SELECT AVG(total) FROM (SELECT     COUNT(t.name) total FROM backend.DataTable t GROUP BY     DATE(actiontime)) result]

但是下面的 SQL 工作正常:

SELECT AVG(total) FROM (SELECT COUNT(NAME) total FROM DATA_TABLE GROUP BY DATE(actiontime)) result 

如果我理解正确的话,JPQL 在子查询方面有问题。那么我应该如何创建这种查询?

最佳答案

我认为 jpql 不支持带有 from 子句的 subselect。根据 jpa docs

Subqueries are restricted to the WHERE and HAVING clauses in this release. Support for subqueries in the FROM clause will be considered in a later release of the specification.

您可以在 @Query 注释中使用 nativeQuery = true 并将其作为 native 查询运行,或者在可能的情况下重写查询。

关于spring-data-jpa - JPQL @Query 导致 "unexpected token: (",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36233108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com