当前位置:网站首页>SQL数据分析之流程控制语句【if,case...when详解】
SQL数据分析之流程控制语句【if,case...when详解】
2022-07-01 23:59:00 【Chung丶无际鹰】
文章目录
零、写在前面
本文所有代码均是在SQL ZOO平台进行,数据也该平台下的world表,所有代码均已通过测试。
一、if单条件判断语句
1、语法结构:if(判断条件, 成立值, 不成立值)
select if(1=1,'Yes','No');
2、判断亚洲和非洲中国土面积大于200万的国家是否为人口大国(人口大于2亿)
select continent,name,if(population>200000000,'人口大国','小国') 人口规模
from world
where continent in ('Asia','Africa') and area>2000000
order by continent
执行结果:
3、查询国土面积大于100万的亚洲国家中的总体经济水平(大于1万亿为高经济体)
select name,gdp,if(gdp>1000000000000,'高经济体','低经济体') as 总体经济
from world
where continent='Asia' and area>1000000
二、case…when多条件判断
1、单条件(if…else效果)
将上述if修改成case…when:
#判断亚洲和非洲中国土面积大于200万的国家是否为人口大国(人口大于2亿)
select continent,name,
CASE
WHEN population>200000000 THEN '人口大国'
ELSE '小国'
END AS 人口规模
from world
where continent in ('Asia','Africa') and area>2000000
order by continent
#查询国土面积大于100万的亚洲国家中的总体经济水平(大于1万亿为高经济体)
select name,gdp,
CASE
when gdp>1000000000000 then '高经济体'
else '低经济体'
END as 总体经济
from world
where continent='Asia' and area>1000000
2、多条件(if…else if…else效果)
从gdp粗略判断国土面积大于50万,人口大于2000万的亚洲和欧洲国家的状态:
select continent,name,population,gdp,
CASE
WHEN gdp>=3000000000000 THEN '一等大国'
WHEN gdp>=2000000000000 and gdp<=3000000000000 THEN '二等大国'
WHEN gdp>=200000000000 and gdp<=1000000000000 THEN '三等大国'
ELSE '发展中大国'
END AS 国家状态
from world
where continent in ('Asia','Europe') and area>500000 and population>20000000
order by continent
3、常量用法(switch…case效果)
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1
when 常量2 then 要显示的值2或语句2
…
else 要显示的值n或语句n;
例如:把人口数大于5000万国家的大洲名翻译成对应的中文
select name,population,
CASE continent
WHEN 'Asia' THEN '亚洲'
WHEN 'Afica' THEN '非洲'
WHEN 'South America' THEN '南美洲'
WHEN 'North America' THEN '北美洲'
WHEN 'Europe' THEN '欧洲'
ELSE '其他大洲'
END AS 所属大洲
from world
where population>=50000000
三、ifnull填充空值
Ifunull(字段名,“值1”)如果说这列的内容为null,则用’'来填充。如果不是null的话,则显示原有值。
select id, ifnull('score','缺考') as 分数;
若分数为null,则表示该学生缺考。
边栏推荐
- 【ES实战】ES上的安全性运行方式
- 关联性——组内相关系数
- ADO.NET 之sqlConnection 对象使用摘要
- SQL optimization
- Leetcode96 different binary search trees
- [Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
- Selectively inhibiting learning bias for active sampling
- leetcode96不同的二叉搜索树
- Chapter 6 data flow modeling
- How to realize parallel replication in MySQL replication
猜你喜欢
leetcode96不同的二叉搜索樹
Algolia's search needs are almost closed
ConcurrentSkipListMap——跳表原理
【QT】测试Qt是否能连接上数据库
回顾数据脱敏系统
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
Deep learning | three concepts: epoch, batch, iteration
2021 robocom world robot developer competition - preliminary competition of undergraduate group
Graduation season is both a farewell and a new beginning
Chapter 6 data flow modeling
随机推荐
[QT] QT cannot find a solution to the compiler using msvc2017
.env.xxx 文件,加了常量,却undefined
2021 robocom world robot developer competition - semi finals of higher vocational group
记录一下大文件上传偶然成功偶然失败问题
使用VB.net将PNG图片转成icon类型图标文件
Download the online video m3u8 tutorial
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
Jielizhi Bluetooth headset quality control and production skills [chapter]
下载在线视频 m3u8使用教程
Resumption of attack and defense drill
golang中的iota
启牛学院开户安全的吗?开户怎么开?
微信小程序缓存过期时间的相关设置(推荐)
[QT] qtcreator uninstall and installation (abnormal state)
Selectively inhibiting learning bias for active sampling
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
[QT] test whether QT can connect to the database
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
Jielizhi, production line assembly link [chapter]
Relevant settings of wechat applet cache expiration time (recommended)