请查找以下查询。 这个查询是我在 spring data jpa 中编写的。所以它应该适用于所有关系数据库,但我在 MsSQL Server 上遇到异常,而在 MySQL 中它工作正常。
@Query(value = "SELECT p.ct,p.ctc,p.fy,"
+ " sum(p.oA + p.nA) as a,p.py "
+ " FROM P p where p.py = :py AND p.fy = :fy "
+ " AND p.B.eId in (SELECT eId FROM B WHERE o.oId in ( :oIds ) ) group by p.ct")
List<?> test(@Param("py")String py,@Param("fy") Integer fy,@Param("oIds")List<Integer> oIds);
这完全可以在 MySQL 服务器上运行,但不能在 MsSQL 服务器上运行。
当我在 MsSQL 中运行时,它给出了以下异常。
SqlExceptionHelper: Column 'tbl.ctc' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
[2017-10-27 09:38:38.108] boot - 83734 ERROR [http-bio-8080-exec-1] ---
任何帮助都值得赞赏。
谢谢
最佳答案
这不是 Spring Data JPA 的问题。您正在使用不属于 SQL 标准的 MySQL 特定功能:
10.6 General Rules for the GROUP BY Clause
If a select block does have a GROUP BY clause, any column specification specified in the SELECT clause must exclusively occur as a parameter of an aggregated function or in the list of columns given in the GROUP BY clause, or in both.
您的查询有
SELECT p.ct, p.ctc, p.fy, p.pt
只有
group by p.ct"
通常这是无效的 SQL。这就是为什么许多其他数据库(MySQL 除外)会抛出异常。
将列添加到 group by
子句或使用列的聚合函数。
关于java - Spring Data JPA 查询无法在 MsSQL Server 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46979527/