1

尝试使用 NHibernate 运行以下 HQL:

select count(distinct t) as TweetCount
from Tweet t
    join t.Tweeter u
    left join t.Votes v
    left join t.Tags tag
where t.App = :app
having count(distinct v) > 0

但是由于某种原因,有条款被忽略了,当只有 2 条推文有投票权时,它会计算所有推文。我基本上想计算至少有一个投票的推文数量。

这是我的数据库我的推特数据库

我尝试向我的查询添加一个 group by,如下所示:

select count(distinct t) as TweetCount
from Tweet t
    join t.Tweeter u
    left join t.Votes v
    left join t.Tags tag
where t.App = :app
group by t
having count(distinct v) > 0

...但它最终返回了一个包含 2 个整数的集合,每个整数都设置为 '1' 而不是唯一的结果。

4

1 回答 1

3

这符合我们的要求

select count(distinct t.Id) as TweetCount
from Tweet t
    inner join t.Votes v
where t.App = :app

由于我们是在内部加入投票表,因此任何没有投票的推文都不会计入结果集。

使用纯 HQL 语法的另一种方法是

select count(distinct t.Id) as TweetCount
from Tweet t
where t.App = :app and size(t.Votes) > 0

这将根据您的方言创建一个 sql 语句,size() 函数是特定于 hql 的集合,请参见 NHibernate 参考中的 13.8

于 2010-10-07T07:17:25.933 回答