简体   繁体   English

(JPQL) - 查询获取最多记录数的用户

[英](JPQL) - Query for getting user with highest number of records

Excuse me for anking again about this issue but I need to have a JPA query for this: 请原谅我再次讨论这个问题,但我需要有一个JPA查询:

select username, count(*)
from Records
group by username
order by count(*) desc
limit 1

I thought about smth like: 我想到了像:

select r.username,count(*) from Records r order by r.username desc

and then to call 然后打电话

getResultList().get(0)

but I am allowed to write only: 但我只能写:

select r from Records r order by r.username desc

and in this case I do not know how to get what I need. 在这种情况下,我不知道如何得到我需要的东西。 Does anyone have any idea? 有人有什么主意吗?

The SQL query has a group by, and orders by count. SQL查询有一个group by和order by count。 The JPA query doesn't have any group by and orders by user name. JPA查询没有任何group by和用户名order。 So I don't see how they could return the same thing. 所以我不知道他们怎么能回归同样的事情。

The equivalent JPQL query is 等效的JPQL查询是

select r.username, count(r.id)
from Record r
group by r.username
order by count(r.id) desc

If you call setMaxResults(1) on the Query object, the limit clause will be added to the generated SQL query, making it completely equivalent. 如果在Query对象上调用setMaxResults(1) ,则limit子句将添加到生成的SQL查询中,使其完全等效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM