我有一个数据模型,其中一个账户可以有多个用户:
class Account {
Long id;
}
class User {
Long id;
@ManyToOne
Account account;
}
我想这样做,显示每个帐户的用户数以下查询:
select Account.id, NumUsers.num from Account,
(select Account.id as account_id, count(User.id) as num
from User join Account on User.account_id=Account.id
group by Account.id) as NumUsers
where Account.id=NumUsers.account_id;
我知道我可以重新编写这个特定的查询为:
select Account.id, count(User.id) from Account join
User on User.account_id=Account.id group by Account.id
但我打算创建需要由多个组报告更复杂的查询。 我读到这里有关正确的方法进行多组。
如何创建使用JPA2标准API我的查询?