首页 文章

数据库查询选择所有列,包括每个记录的计数'group by'

提问于
浏览
0

使用Criteria或HQL(Hibernate Query Language),选择 all columns . Grouping by 所选列的任意一列,并选择 count 与我们使用 group by 的每条记录 .

SQL查询示例ex: *select ,count(1) from users group by name

1 回答

  • 0

    你试过运行你的那个SQL吗?你不能运行这个查询吗?

    select *,count(1) from users group by name
    

    你会重现一些错误:

    Column 'some_column_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
    

    你能做的就是查询这样的东西:

    select name, count(name) from users group by name
    

    在这种情况下,您可以在条件中使用投影来计算group by,例如:

    criteria.setProjection(Projections.projectionList()
        .add(Projections.groupProperty("name"))
        .add(Projections.count("name")));
    

相关问题