JPA 有没有办法做类似 SELECT, count(*) FROM的事情?分组依据<字段>

发布于 2024-09-03 16:55:55 字数 246 浏览 4 评论 0原文

我一直在网上寻找有关计数等聚合的示例,但似乎它们都单独使用聚合。

SELECT field, count(*) FROM table GROUP BY field

Should have something like:

field.value1, x1
field.value2, x2
....

我正在寻找一个纯粹的 JPA 答案。如果不是,我想我可以对计数部分进行进一步的查询,但这似乎效率不高。

有什么想法吗?

I've been looking in the web for examples on the aggregates like count but it seems all of them are using the aggregate alone.

SELECT field, count(*) FROM table GROUP BY field

Should have something like:

field.value1, x1
field.value2, x2
....

I'm looking for a pure JPA answer for this one. If not I guess I can then do further queries just for the count part but that seems unefficient.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-09-10 16:55:55

我不确定我是否正确理解了这个问题,但以下 JPQL 查询是否符合您的要求:

SELECT p.name, count(p) from Product p group by p.name

您可以像这样检索数据:

List datos=(List)query.getResultList();

其中 Object[0] 是第一个字段,Object[1] 是第二个字段,依此类推。

I'm not sure I understood the question correctly but doesn't the following JPQL query do what you want:

SELECT p.name, count(p) from Product p group by p.name

You can retrieve the data like this:

List datos=(List)query.getResultList();

where Object[0] is the first field, Object[1] the second and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文