JPQL有什么问题(计数和选择)

时间:2015-06-03 02:16:15

标签: sql oracle join count subquery

以下是从父表中获取行的查询以及从子表中获取每个id的计数。

@Query("SELECT cnt idcount, person FROM Person person,(SELECT COUNT(child.id) cnt FROM Child child WHERE child.id :input_id) idcount WHERE person.id :input_id")
List<Person> searchPersonWithCount(@Param("input_id") Long input_id);

我得到以下错误:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: idcount near line 1, column 12 [SELECT cnt idcount, person FROM com.myc.cc.domain.Person person,(SELECT COUNT(child.id) cnt FROM com.myc.cc.domain.Child child WHERE child.id :input_id) idcount WHERE person.id :input_id]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)

当我执行这个是在oracle sqldeveloper中我能够得到结果。

在Qracle sql developer中执行本机查询:

select  cnt idcount, person.* from Person person,(select count(*) cnt from Child child WHERE id = '1235') idcount WHERE id = '1235'

结果:

IDCOUNT         ID          NAME
   2          1235         PersonA

任何人都可以让我知道JPQL中有什么问题。

提前致谢。

示例数据:

父母表:

ID         NAME

1001      sampleNameA
1002      sampleNameB
1003      sampleNameC

儿童表:

ID        NAME       INTERESTS

1001     sampleNameA    music   
1001     sampleNameA    dance
1001     sampleNameA    coding
1003     sampleNameC    music   
1002     sampleNameB    dance
1002     sampleNameB    coding

如果id = 1001,

预期输出

COUNT   ID         NAME

3   1001      sampleNameA

0 个答案:

没有答案