HQL子查询报错:org.hibernate.hql.ast.QuerySyntaxException: unexpected token...

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gingerredjade/article/details/88877717

想统计某类别数据的数量,于是写了个子查询,HQL内容大致如下:

select count(*) from (select stu.classId  from Student stu group by stu.classId) 

执行该HQL语句一直报错:org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 22 [select count(*) from (...  

反复检查该HQL语句,没发现问题;在数据库中执行对应的sql语句,也能正确得到结果;

不接问题原因,最后看到一篇帖子说到

《hibernate_reference.pdf》《第14章 HQL: Hibernate查询语言》。猛然瞥见《14.13. 子查询》小节倒数第二段:

Note that HQL subqueries can occur only in the select or where clauses

原来“HQL子查询只能出现在select或where字句中“ 


由于使用了Hibernate,最终只能使用如下HQL解决,其实这个更简便:

select count(distinct stu.classId)  from Student

部分描述参照文章:https://blog.csdn.net/u013972119/article/details/31835333 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/gingerredjade/article/details/88877717