jesusjsc

MySQL中的几个控制流函数
# MySQL中的几个控制流函数 --- ## CASE ``` CASE value WHE...
扫描右侧二维码阅读全文
22
2019/04

MySQL中的几个控制流函数

MySQL中的几个控制流函数


CASE

CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END

value = compare_value时,返回result

CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END

当满足condition时,返回result

IF(expr1, expr2, expr3)

如果expr1为真(expr1 <> 0 and expr1 <> null),那么if()返回expr2,否则返回expr3

mysql> SELECT IF(1>2,2,3);
        -> 3
mysql> SELECT IF(1<2,'yes','no');
        -> 'yes'
mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
        -> 'no'

IFNULL(expr1, expr2)

如果expr1 is not null,那么返回expr1,否则返回expr2

mysql> SELECT IFNULL(1,0);
        -> 1
mysql> SELECT IFNULL(NULL,10);
        -> 10
mysql> SELECT IFNULL(1/0,10);
        -> 10
mysql> SELECT IFNULL(1/0,'yes');
        -> 'yes'

NULLIF(expr1, expr2)

如果expr1 = expr2为真,那么返回NULL,否则返回expr1

mysql> SELECT NULLIF(1,1);
        -> NULL
mysql> SELECT NULLIF(1,2);
        -> 1
Last modification:June 23rd, 2019 at 11:22 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment