使用mysql sum统计函数 结果为null时返回值改为0

一时失言乱红尘 2023-10-17 09:38 151阅读 0赞
  1. //查询定时任务是否存在
  2. public Integer getPetTime(HomePetTime homePetTime){
  3. Integer result = 0;
  4. ConnectionDB util = new ConnectionDB();
  5. Object[] objs =new Object[]{homePetTime.getGateway_id(),homePetTime.getTiming_id()};
  6. List set= util.excuteQuery("select COALESCE(sum(1),0) as times from wlsq_data.home_pet_feeder_timing where gateway_id=? and timing_id=? and del_flag=0 ", objs);
  7. if(set !=null && set.size() >0){
  8. for(Object obj:set){
  9. HashMap<String, Object> map =(HashMap<String, Object>)obj;
  10. Integer str = Integer.parseInt(map.get("times").toString());
  11. if(str == 0){
  12. result = 0;
  13. }else{
  14. result = str;
  15. }
  16. }
  17. }
  18. return result;
  19. }

重点核心sql:

  1. select COALESCE(sum(1),0) as times from wlsq_data.home_pet_feeder_timing where gateway_id=? and timing_id=? and del_flag=0

发表评论

表情:
评论列表 (有 0 条评论,151人围观)

还没有评论,来说两句吧...

相关阅读