当前位置: 移动技术网 > IT编程>数据库>Mysql > 浅谈Mysql中类似于nvl()函数的ifnull()函数

浅谈Mysql中类似于nvl()函数的ifnull()函数

2017年12月12日  | 移动技术网IT编程  | 我要评论

价格表,w88优德89168澳门官方,任柯诺的老婆

ifnull(expr1,expr2)

如果expr1不是null,ifnull()返回expr1,否则它返回expr2。ifnull()返回一个数字或字符串值,取决于它被使用的上下文环境。

mysql> select ifnull(1,0);
    -> 1
mysql> select ifnull(0,10);
    -> 0
mysql> select ifnull(1/0,10);
    -> 10
mysql> select ifnull(1/0,'yes');
    -> 'yes'
 
if(expr1,expr2,expr3) 

如果expr1是true(expr1<>0且expr1<>null),那么if()返回expr2,否则它返回expr3。if()返回一个数字或字符串值,取决于它被使用的上下文。

mysql> select if(1>2,2,3);
    -> 3
mysql> select if(1<2,'yes','no');
    -> 'yes'
mysql> select if(strcmp('test','test1'),'yes','no');
    -> 'no'

expr1作为整数值被计算,它意味着如果你正在测试浮点或字符串值,你应该使用一个比较操作来做。

mysql> select if(0.1,1,0);
    -> 0
mysql> select if(0.1<>0,1,0);
    -> 1

在上面的第一种情况中,if(0.1)返回0,因为0.1被变换到整数值, 导致测试if(0)。这可能不是你期望的。在第二种情况中,比较测试原来的浮点值看它是否是非零,比较的结果被用作一个整数。

case value when [compare-value] then result [when [compare-value] then result ...] [else result] end 
  
case when [condition] then result [when [condition] then result ...] [else result] end 

第一个版本返回result,其中value=compare-value。第二个版本中如果第一个条件为真,返回result。如果没有匹配的result值,那么结果在else后的result被返回。如果没有else部分,那么null被返回。

mysql> select case 1 when 1 then "one" when 2 then "two" else "more" end;
    -> "one"
mysql> select case when 1>0 then "true" else "false" end;
    -> "true"
mysql> select case binary "b" when "a" then 1 when "b" then 2 end;
-> null

以上这篇浅谈mysql中类似于nvl()函数的ifnull()函数就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网