玖叶教程网

前端编程开发入门

十、mysql中ifnull、isnull、nullif、if、elt的使用及区别

1、ifnull(expression, value)

ifnull函数用于判断第一个参数expression表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。

Expression:要测试的值或表达式

Value:若expression为空,则返回Value

若Expression为字段,那么Expression字段的默认值需要为null,否则ifnull会不生效

select id,name,other,ifnull(other,'若other为空就返回我')  from user

测试结果:


2、isnull(filed)

判断字段是否为空,是则返回1,否则返回0

select id,name,other,isnull(other) from `user`

测试结果:

3、nullif(filedA,filedB)

主要用来看表字段的数据是否相同的。当二个字段数据相同,该函数就会返回null,如果不相同,则会返回第一个参数的值。

select id,name,infornation,other,nullif(infornation,other) from `user`

测试结果:


4、if(expr1,expr2,expr3);

如果expr1为true,则if()返回值为expr2,否则返回值为expr3

select id,name,infornation,if(infornation = '学生',1,0) from user

测试结果:


5、elt(N,str1,str2,str3,...)

如果N = 1,则返回str1;如果N = 2,则返回str2,依此类推。 如果N小于1或大于参数个数,则返回NULL。 ELT是FIELD的补充。

select id,code,name,elt(code,'奖励1W','奖励3W','奖励5W') as '奖励等级' from user

测试结果:

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言