Home > Database > Mysql Tutorial > mysql中的if与case-when

mysql中的if与case-when

PHPz
Release: 2018-09-30 14:05:53
forward
2703 people have browsed it

一、if

1.格式

<span style="color: rgb(0, 0, 0);">IF(expr1,expr2,expr3)<br></span>
Copy after login

2.作用
如果expr1是true(expr1不等于0且不是null),那么IF的返回值是expr2,否则返回expr3

非常类似于  result1==result2?expr1:expr2

3.实例

<span style="color: rgb(0, 0, 0);">SELECT<br>        IF (o.sex = 1, '男', '女')<br>FROM<br>	sys_user o<br></span>
Copy after login

如果sex字段为1,就返回男,否则返回女

二、case when

1.实例

<span style="color: rgb(0, 0, 0);">SELECT<br>	CASE o.sex<br>     WHEN 1 THEN<br>	       '男'<br>     WHEN 0 THEN<br>	       '女'<br>     ELSE<br>	       '未定义'<br>  END<br>FROM<br>	sys_user o<br></span>
Copy after login

如果1返回男,为0返回女,否则返回未定义

更多相关教程请访问 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