我正在咨询http://www.querydsl.com/static/querydsl/3.2.1/reference/pdf/Querydsl_Reference.pdf
的文档我看到了示例
QDepartment department = QDepartment.department;
QDepartment d = new QDepartment("d");
query.from(department)
.where(department.employees.size().eq(
new JPASubQuery().from(d).unique(d.employees.size().max())
)).list(department);
我尝试使用下面的代码使用我的表执行查询:
QAdminEntity tableAdmin = QAdminEntity.adminEntity;
JPAQuery query = queryFrom( tableAdmin ).where(
tableAdmin.id_city.eq( idCity ).and(
tableAdmin.problems.size().eq(
subQueryFrom( tableAdmin ).unique(
tableAdmin.problems.size().min()
)
)
)
);
return query.singleResult( tableAdmin );
该代码给我一个错误,例如“无法嵌套聚合函数调用”。
我很遗憾因为缺乏postgres和querydsl的专业知识,但我认为这应该非常简单。
结果查询:
select
adminentit0_.id_admin as id1_3_,
adminentit0_.ds_email as ds2_3_,
adminentit0_.ds_name as ds3_3_,
adminentit0_.ds_password as ds4_3_,
adminentit0_.ds_username as ds5_3_,
adminentit0_.id_city as id6_3_,
adminentit0_.id_permission as id7_3_
from
m_admin adminentit0_
where
adminentit0_.id_city=?
and (
select
count(problems1_.id_admin)
from
m_problem problems1_
where
adminentit0_.id_admin=problems1_.id_admin
)=(
select
min(count(problems3_.id_admin))
from
m_admin adminentit2_,
m_problem problems3_
where
adminentit2_.id_admin=problems3_.id_admin
) limit ?
正如您所看到的,我想列出管理员的问题比关系管理员中的其他人少 - >问题。
我该如何做到这一点?
问题:
是否有必要为子查询获取另一个“QAdminEntity”字段,或者“新QDepartment(”d“)代码中的”d“变量仅用于示例?
答案 0 :(得分:1)
以下查询应具有类似的语义
QAdminEntity adminEntity = QAdminEntity.adminEntity;
JPAQuery query = query.from(adminEntity)
.where(adminEntity.id_city.eq(idCity))
.orderBy(adminEntity.problems.size().asc())
.limit(1);