Home > Database > Mysql Tutorial > MySQL条件判断函数

MySQL条件判断函数

PHPz
Release: 2018-09-30 11:55:29
forward
1353 people have browsed it

1、IF(expr, v1, v2)函数

如果expr成立,返回值为v1,否则返回v2

mysql> select if(1>2,2,3),    -> if(1<2,'yes','no'), -> if(strcmp('test','test1'),'no','yes');
+-------------+--------------------+---------------------------------------+| if(1>2,2,3) | if(1<2,'yes','no') | if(strcmp('test','test1'),'no','yes') |
+-------------+--------------------+---------------------------------------+|           3 | yes                | no                                    |
+-------------+--------------------+---------------------------------------+1 row in set (0.00 sec)

2.IFNULL(v1, v2)函数

IFNULL(v1,v2)假如v1不为NULL,则IFNULL()的返回值为v1;否则其返回值为v2。IFNULL()的返回值是数字或是字符串,具体情况取决于其所在的语境。

mysql> select ifnull(1,2),ifnull(NULL,10),ifnull(1/0,&#39;wrong&#39;);
+-------------+-----------------+---------------------+| ifnull(1,2) | ifnull(NULL,10) | ifnull(1/0,&#39;wrong&#39;) |
+-------------+-----------------+---------------------+|           1 |              10 | wrong               |
+-------------+-----------------+---------------------+1 row in set (0.00 sec)
Copy after login

3.CASE函数

CASE expr WHEN v1 THEN r1 [WHEN v2 THEN r2] [ELSE rn] END
该函数表示,如果expr值等于某个vn,则返回对应位置THEN后面的结果。如果与所有值都不相等,则返回ELSE后面的rn。

mysql> select case 2 when 1 then &#39;one&#39; when 2 then &#39;two&#39; else &#39;more&#39; end;
+------------------------------------------------------------+| case 2 when 1 then &#39;one&#39; when 2 then &#39;two&#39; else &#39;more&#39; end |
+------------------------------------------------------------+| two                                                        |
+------------------------------------------------------------+1 row in set (0.00 sec)
Copy after login

更多相关教程请访问 MySQL视频教程

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template