当前位置:网站首页>MySQL 流程控制
MySQL 流程控制
2022-08-03 05:39:00 【知其黑、受其白】
阅读目录
MySQL流程控制 IF()、IFNULL()、NULLIF()、ISNULL()函数的使用
MySQL IF() 函数的使用
SELECT IF(TRUE,'A','B'); -- 输出结果:A
SELECT IF(FALSE,'A','B'); -- 输出结果:B
SELECT
id,
title,
IF (
url IS NULL,
'没奖金呵呵',
'有奖金嘻嘻'
) AS 链接
FROM
video;
mysql> select if(true,'hello','world');
+--------------------------+
| if(true,'hello','world') |
+--------------------------+
| hello |
+--------------------------+
1 row in set (0.00 sec)
mysql> select if(false,'hello','world');
+---------------------------+
| if(false,'hello','world') |
+---------------------------+
| world |
+---------------------------+
1 row in set (0.00 sec)
分支结构之 IF
IF 表达式1 THEN 操作1
[ELSEIF 表达式2 THEN 操作2]……
[ELSE 操作N]
END IF
IFNULL() 函数的使用
SELECT IFNULL(NULL,'B'); -- 输出结果:B
SELECT IFNULL('HELLO','B'); -- 输出结果:HELLO
NULLIF() 函数的使用
SELECT NULLIF('A','A'); -- 输出结果:null
SELECT NULLIF('A','B'); -- 输出结果:A
ISNULL() 函数的使用
SELECT ISNULL(NULL); -- 输出结果:1
SELECT ISNULL('HELLO'); -- 输出结果:0
case 函数
CASE 表示函数开始,END 表示函数结束。
如果 condition1 成立,则返回 result1,
如果 condition2 成立,则返回 result2,
当全部不成立则返回 result,而当有一个成立之后,后面的就不执行了。
CASE expression
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
WHEN conditionN THEN resultN
ELSE result
END
传参的写法:
不传参的写法
实操案例
准备数据
-- 创建订单表
CREATE TABLE orders (
oid INT PRIMARY KEY,
-- 订单id
price DOUBLE,
-- 订单价格
payType INT -- 支付类型(1:微信支付 2:支付宝支付 3:银行卡支付 4:其他)
);
insert into orders values(1,1200,1);
insert into orders values(2,1000,2);
insert into orders values(3,200,3);
insert into orders values(4,3000,1);
insert into orders values(5,1500,2);
示例 1
select
*,
case
when payType=1 then "微信支付"
when payType=2 then "支付宝支付"
when payType=3 then "银行卡支付"
else "其他支付方式"
end as payTypeStr
from orders;
示例 2
select
*,
case payType
when 1 then "微信支付"
when 2 then "支付宝支付"
when 3 then "银行卡支付"
else "其他支付方式"
end as payTypeStr
from orders;
上述分别是用了等值判断和值的映射去实现,一般我更喜欢使用第一种,因为有时候还可以范围运算。
边栏推荐
- empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType),
- mysql 时间字段默认设置为当前时间
- torch.nn.modules.activation.ReLU is not a Module subclass
- SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
- AlexNet网络详解及复现
- html+css+php+mysql实现注册+登录+修改密码(附完整代码)
- El - tree set using setCheckedNodessetCheckedKeys default check nodes, and a new check through setChecked specified node
- 信息学奥赛一本通T1453:移动玩具
- 重量级大咖来袭:阿里云生命科学与智能计算峰会精彩内容剧透
- 信息学奥赛一本通T1448:深搜的剪枝技巧 电路维修
猜你喜欢
随机推荐
El - tree set using setCheckedNodessetCheckedKeys default check nodes, and a new check through setChecked specified node
重量级大咖来袭:阿里云生命科学与智能计算峰会精彩内容剧透
【EA Price strategy OC1】以实时价格为依据的EA,首月翻仓!】
单节点部署 gpmall 商城系统(一)
ES6中 Symbol 的基础学习,迭代器和生成器的基本用法
Autowired注解与Resource注解的区别
El - table column filter functions, control columns show and hide (effect and easy to implement full marks)
pyspark df 二次排序
SQLSERVER将子查询数据合并拼接成一个字段
Docker-compose安装mysql
spark中Repartition 和 Coalesce 区别
Getting started with el-tabs (tab bar)
FiBiNet torch复现
PostMan测试接口-----上传文件、导出excel
1066 Root of AVL Tree // AVL平衡二叉搜索树模板
配置MSTP功能示例
Chrome插件开发入门
MySQL 日期时间类型精确到毫秒
Pinned Articles-
pyspark @udf 循环使用变量问题