1. 程式人生 > >使用mysql sum統計函式 結果為null時返回值改為0

使用mysql sum統計函式 結果為null時返回值改為0

//查詢定時任務是否存在
	public Integer getPetTime(HomePetTime homePetTime){
		Integer result = 0;
		ConnectionDB util = new ConnectionDB();
		Object[] objs =new Object[]{homePetTime.getGateway_id(),homePetTime.getTiming_id()};
		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);
		if(set !=null && set.size() >0){
			for(Object obj:set){			
				HashMap<String, Object> map =(HashMap<String, Object>)obj;					
					Integer str = Integer.parseInt(map.get("times").toString());
					if(str == 0){
				    	result = 0;
				    }else{
				    	result = str;
				    }
				
				
			}		
			    
										
		}		
		return result;	
		
	}

重點核心sql:

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