我们今天主要和大家一起讨论的是mysql字符串值与用其实际表达式来对函数 ifnull()与if() [类似ms sql server的isnull()]进行正确判断的实际操作步骤的介绍,以下就是正文的主要内容描述。 fnull(expr1,expr2) 如果expr1不是null,ifnull()返回expr1,否则它
我们今天主要和大家一起讨论的是mysql字符串值与用其实际表达式来对函数 ifnull()与if() [类似ms sql server的isnull()]进行正确判断的实际操作步骤的介绍,以下就是正文的主要内容描述。
fnull(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(expr10且expr1null),那么if()返回expr2,否则它返回expr3。if()返回一个数字或字符串值,取决于它被使用的上下文。
mysql> select if(1>2,2,3); -> 3 mysql> select if(12,’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 字符串值/表达式判断函数ifnull()与if() [类似ms sql server的isnull()]的介绍,望你能有所收获。