IFNULL(字段1,字段2),意思是当字段1是NULL时取字段2,不是NULL时取字段1的值。
举例: ifnull(schooladdress,homeaddress) address
以下代码摘自博客园仅供参考:
StringBuilder sb=new StringBuilder();
sb.append(" select ");
sb.append("t1.fullName,IFNULL(t2.actualHour,0)as actualHour,t1.planhour,t1.annotatorId,t1.annotatorId,IFNULL(t2.actualHour,0)/t1.planhour as ratio");
sb.append(" from ");
sb.append(" XXshipTB t1 ");
sb.append(" left outer join (select userid,sum(hours) as actualHour from dayworkTB where (Date(date) between '"+fromDate+"' and '"+endDate+"') group by userid) t2");
sb.append(" on ");
sb.append(" t1.annotatorId=t2.userid");
sb.append(" and abs(t2.actualHour-t1.planhour)>0.01");
sb.append(" ORDER by ");
sb.append(" ratio desc,t1.fullName asc");
String sql=sb.toString();
本文详细解析了IFNULL函数的使用方法,该函数在字段为NULL时返回备选值,避免了空值带来的错误。通过具体代码示例,展示了在SQL查询中如何运用IFNULL函数处理数据,确保计算的准确性。
1191

被折叠的 条评论
为什么被折叠?



