MySQL的IFNULL函数
2015-09-14 13:59
691 查看
MySQL函数里有一个很有用的函数IFNULL,它的形式是IFNULL(fieldA,fieldB),意义是当字段fieldA是NULL时取fieldB,不是NULL时取fieldA的值。
这个函数与外连接配合使用时能起到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();
这个函数与外连接配合使用时能起到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();
相关文章推荐
- MySQL学习笔记-锁相关话题
- MySQL优化—DBA对MySQL优化的一些总结
- mysql 显示行号,以及分组排序
- mysql如何实现插入数据时如果不存在则插入如果存在则更新的操作
- Navicat for mySQL 10.1.7注册码
- mysql--数据类型和常用字段属性总结
- mysql ---编码问题
- Mysql数据库主从同步
- 解决mysql数据库大小写敏感问题【mysql数据库和表其实是数据目录下的目录和文件,所以操作系统的敏感性决定数据库和表命名的大小写敏感】
- 解决MySQL不允许(其它IP)从远程访问的方法
- Mysql 5.7密码过期修改密码
- mysql mybaits批量更新时遇到的一个小问题
- JDBC连接MySQL数据库
- MySQL错误Another MySQL daemon already running with the same unix socket.v
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
- mysql中bin-log日志操作常用命令
- mysql替换空格
- mysql查看数据库和表的占用空间大小
- excel文档导入mysql表步骤
- MYSQL 主从服务器配置