3

我想执行下面的 SQL

select count(*) as myCount from user group by name;

我想出了以下相同的标准

DetachedCriteria.ForClass(typeof(UserDTO))
    .setProjections(Projections.ProjectionList()
                        .Add(Projections.rowCount(),"myCount")
                        .Add(Projections.groupProperty("this.name"));

我将结果作为计数和名称返回,我怎样才能从中得到计数。

4

2 回答 2

0

我不认为您可以使用 Criteria 来做到这一点,但使用 HQL 很容易。它与您的 SQL 查询完全相同,但使用实体/属性名称而不是表/列名称。

于 2010-10-13T00:25:19.587 回答
0

如果按列只有一组,则可以使用 count distinct。

总部:

select count(distinct name) as myCount from user

标准:

DetachedCriteria.ForClass(typeof(UserDTO))
.setProjections(Projections.ProjectionList()
                    .Add(Projections.countDistinct("name"),"myCount"));
于 2014-07-22T07:26:31.930 回答