MySQL的IFNULL函数
MySQL函數(shù)里有一個很有用的函數(shù)IFNULL,它的形式是IFNULL(fieldA,fieldB),意義是當字段fieldA是NULL時取fieldB,不是NULL時取fieldA的值。
這個函數(shù)與外連接配合使用時能起到Oracle的IIF或是NVL的作用:
以下代碼供參考:
??????? 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();
轉載于:https://www.cnblogs.com/xiandedanteng/p/3329053.html
總結
以上是生活随笔為你收集整理的MySQL的IFNULL函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# is和as的区别
- 下一篇: sql 定时同步两个数据库