IFNULL equivalent in Hibernate Query Language?

2019-03-18 17:27发布

I'm trying to write an HQL query which will calculate an average rating for an item. I want the query to return 0 instead of null when there are no rating for a given item - so that I can use my query as a subquery. So is it possible? Is there an HQL equivalent of IFNULL or NVL?

2条回答
再贱就再见
2楼-- · 2019-03-18 18:09

COALESCE is the official equivalent.

It returns the first non-null of its arguments.

Example:

    COALESCE(id_pati, 0)

Link Wikipedia

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-18 18:13

Nhibernate docs are out of date. Check http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql.html

If nothing works you can try:

select 
   case 
     when something is not NULL then 0
     else 
   end
查看更多
登录 后发表回答