Hibernate HqlL:计数和分组依据包括零?

Hibernate HqlL: Count and Group By including zero?

我有 2 个 table:声明和 Type_Claim。 Claim 在 Type_Claim 上有一个外部密钥。在 Hibernate 上,表示 Claim table 的 Bean 具有 TypeClaim 作为属性。

  Claim

 ID    TYPE
 1      2
 2      2
 3      4
 4      1

  Type_Claim

 ID    Description
 1     "Hello"
 2     "Hi"
 3     "House"
 4     "Welcome"
 5     "Bye"

现在我做了这个查询:

SELECT tc.description, COUNT(*) 
FROM Claim claim"
LEFT OUTER JOIN claim.typeClaim tc  
GROUP BY tc.description ";

我想得到这个:

Description  Count
  "Hello"     1
  "Hi"        2
  "House"     0
  "Welcome"   1
  "Bye"       0

但是我得到了这个:

Description  Count
  "Hello"     1
  "Hi"        2
  "Welcome"   1

如何在查询中包含 0 个结果?我尝试使用 RIGHT JOIN 但我得到了相同的结果。

聚合函数 count() 不会计算 NULL 值,因此您将得到一个零。

您必须使用 LEFT JOIN 而不是 LEFT OUTER JOIN

如果您想了解有关外部联接的更多信息,这里有一个很好的教程:http://sqlzoo.net/wiki/Using_Null

试试这个:

SELECT tc.description, count(cl.type)
FROM type_claim tc
LEFT OUTER JOIN claim cl ON
cl.type = tc.id
GROUP BY tc.description

对我有用: