HQL-字段为NULL时设置为默认值 0 的写法

case when 属性为空 then 0 else 属性值 end as 属性别名
例子:


@Query(value = "select f.fuel.name as name,case WHEN f.amount is null then 0 else f.amount end as amount,case when f.emissionAmount is null then 0 else f.emissionAmount end as emissionamount," +
        "case when f.lvh is null then 0 else f.lvh end  as lvh ,case when f.oxygenationEfficiency  is null then 0 else f.oxygenationEfficiency end " +
        "as  oxygenationefficiency,case when f.unitHeatCarbonContent is null then 0 else f.unitHeatCarbonContent end as unitheatcarboncontent,f.fuel.id as fuelid from FuelUsed  f" +
        " where f.monthReport.id =?1 and f.unitId =?2 and f.status='1' and f.parentId = ?3")
List<FuelUsedProjection> findUnitFuelUsedInfoList(Long reportId,Long unitId,Long parentId);

 

 

或者可以使用原生sql,mysql的函数为:IFNULL(属性,0),oralce 的函数 nvl(属性,0)

mysql例子

@Query(value = "select  IFNULL(sum(f1.amount),0) as amount,IFNULL(sum(f1.emission_amount),0) as emissionamount,IFNULL(f1.lvh,0) as lvh ,IFNULL(f1.unit_heat_carbon_content,0) AS unitheatcarboncontent," +
        "  IFNULL(f1.oxygenation_efficiency,0) as oxygenationefficiency,f1.fuel_id as fuelid,f2.name as name from fuel_used f1" +
        "  LEFT JOIN  fuel f2 on f1.fuel_id = f2.id " +
        " where f1.month_report_id =?1 " +
        " GROUP BY  f1.lvh,f1.unit_heat_carbon_content,f1.fuel_id,f1.oxygenation_efficiency  order by f1.fuel_id ASC ",nativeQuery = true)
List<FuelUsedProjection> findFuelUsedSummaryInfoList(Long reportId);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值