浅谈Mysql中类似于nvl()函数的ifnull()函数
程序员文章站
2023-12-04 22:24:34
ifnull(expr1,expr2)
如果expr1不是null,ifnull()返回expr1,否则它返回expr2。ifnull()返回一个数字或字符串值,取决于它...
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()函数就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
推荐阅读
-
浅谈Mysql中类似于nvl()函数的ifnull()函数
-
mysql中find_in_set()函数的使用及in()用法详解
-
浅谈js中startsWith 函数不能在任何浏览器兼容的问题
-
Mysql注入中的outfile、dumpfile、load_file函数详解
-
mysql中find_in_set()函数的使用详解
-
MySQL中的LOCATE和POSITION函数使用方法
-
MySQL数据库8——数据库中函数的应用详解
-
MySql 中聚合函数增加条件表达式的方法
-
MySQL中聚合函数count的使用和性能优化技巧
-
ms sql server中实现的unix时间戳函数(含生成和格式化,可以和mysql兼容)