当前位置:网站首页>mysql函数汇总之条件判断函数
mysql函数汇总之条件判断函数
2022-07-18 02:31:00 【运维家】
mysql函数汇总之条件判断函数
条件判断函数也被称为控制流程函数,根据满足的不同条件,执行响应的流程。
mysql中进行条件判断的函数有if、ifunll和case等。
IF(expr,v1,v2)函数
IF(expr,v1,v2):如果表达式expr是TRUE(expr <> 0 and expr <> null),则返回值为V1;否则返回值为V2。
mysql> select if (1>2, 2, 3), if (1<2, 'yes', 'no');
+----------------+-----------------------+
| if (1>2, 2, 3) | if (1<2, 'yes', 'no') |
+----------------+-----------------------+
| 3 | yes |
+----------------+-----------------------+
1 row in set (0.00 sec)
mysql>
小提示:
如果V1或者V2中只有一个明确是
null,则if()函数的结果类型为非null表达式的结果类型。
IFNULL(v1,v2)函数
ifnull(v1,v2):假如V1不为null,则ifnull()的返回值为v1;否则其返回值为v2。
ifnull()的返回值是数字或者字符串,具体情况取决于其所在的语境。
mysql> select ifnull(1, 2), ifnull(null, 'yunweijia'), ifnull(1/0, 'heihei');
+--------------+---------------------------+-----------------------+
| ifnull(1, 2) | ifnull(null, 'yunweijia') | ifnull(1/0, 'heihei') |
+--------------+---------------------------+-----------------------+
| 1 | yunweijia | heihei |
+--------------+---------------------------+-----------------------+
1 row in set (0.00 sec)
mysql>
CASE函数
case expr when v1 then r1 [when v2 then 2]...[else rn+1]end:如果expr值等于某个vn,则返回对应位置then后面的结果;如果与所有值都不相等,则返回else后面的rn+1。
mysql> select case 2 when 1 then 'one' when '2' then 'two' else 'more' end;
+--------------------------------------------------------------+
| case 2 when 1 then 'one' when '2' then 'two' else 'more' end |
+--------------------------------------------------------------+
| two |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql> select case 5 when 1 then 'one' when '2' then 'two' else 'more' end;
+--------------------------------------------------------------+
| case 5 when 1 then 'one' when '2' then 'two' else 'more' end |
+--------------------------------------------------------------+
| more |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
小提示:
可以按照
shell中的if语句来理解。一个
case表达式的默认返回值类型是任何返回值的相容集合类型,但具体情况视其所在语境而定。
至此,本文结束。
边栏推荐
- JMeter 21 day clock in day08
- The unique index exception of the dream database in the Xinchuang environment cannot intercept the duplicatekeyexception
- Is it free to open an account online with flush software? Is it safe to open an account?
- 06. Binary tree and simple application
- [vscode output is garbled] solution
- OS知识点简介(一)
- Dijkstra序列(DAY 66)
- 亿学学堂赠送的账户是哪家证券公司呢?开户安全吗
- Okaleido或杀出NFT重围,你看好它吗?
- 揪心!56岁老父亲为了给孩子筹学费,高温工作9小时去世
猜你喜欢

论文阅读_医疗NLP_ SMedBERT

Entropy technology passed the registration: the annual revenue was 1.955 billion, and the book balance of accounts receivable was 290million

dried food! Integrated fair cold start recommendation system

YoloV7:基于自己训练的模型如何导出正确的ONNX

What is graph neural network?

Uniapp authorized login to obtain user information and code

Torus material mask

OS知识点简介(二)

后缀表达式(DAY 65)

MATLAB学习第四天(决策语句)
随机推荐
Development of face recognition gate based on deep learning (based on paddlepaddle)
QA机器人召回优化
模板的初识
JMeter 21 day clock in Day10
JMeter 21 天打卡 day12
All types of code after await are directly thrown into the micro task queue and executed later
Deep learning environment configuration pytoch
分布式笔记(03)— 分布式缓存之 Redis(单机模式、Redis Cluster 模式概述)
epoll的ET工作模式和LT工作模式
Chapter 4: emerging, class instantiation strategy with constructor based on cglib
个人 IP 实验室周复盘 · 第 19 期
VS2019 List Control 控件自绘例程继承CListCtrl类 OnNMCustomdraw重绘
Weekly resume of personal IP lab · issue 19
[foundation of deep learning] how to calculate convolution
PDF合并后的PDF页面大小不一的解决方法
JMeter 21 day clock in day13
Basic use of anaconda and its use in pychart
使用深度學習制作機器人大腦圖紙
JVM performance tuning method
Does flush software charge for online account opening for stock speculation? Is it safe to open an account?