mysql中的if函数小总

本文介绍如何使用SQL中的IF函数处理NULL值问题,通过具体案例展示如何将NULL转换为0,以便于前端显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

格式:if(conditon.a,b)

解析:当condition为真时,返回a,反之返回b

作用:作为条件语句使用,有时候也可以巧妙的改变从数据库拿出的值;


开发实例如下:String sql = " SELECT s.field_id,IF (s.ifshow IS NULL, 0, s.ifshow) AS ifshow,"
+ "IF (s.ifrequired IS NULL,0,s.ifrequired) AS ifrequired,"
+ "IF (s.ifedit IS NULL, 0, s.ifedit) AS ifedit "
+ "FROM s_template_field s "
+ "where s.template_id ="
+ "(select t.template_id from s_template t "
+ "WHERE t.union_id="
+ "(SELECT d.union_id  from sys_account a ,"
+ "sys_department b , "
+ "s_hospitall_info c ,"
+ "s_union_info d "
+ "where a.USER_ACCT_ID='"+userAccId+"' "
+ "and b.DEPT_ID=a.DEPT_ID  "
+ "and c.hospitall_id=b.HOSPITALL_ID  "
+ "and d.union_id=c.union_id));";

解析:由于数据库中的ifshow ,ifedit,ifrequired默认值为null;用jdbc查询出来以后方便在jsp页面上遍历的话,我们要把null改为0;

这就用到了  if(ifshow IS NULL,0,ifshow);意思就是如果为空的话让它为0,如果不为空就继续为ifshow的值;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值