当前位置:网站首页>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,则表示该学生缺考。
边栏推荐
- BlocProvider为什么感觉和Provider很相似?
- Learn online case practice
- Using SqlCommand objects in code
- S32Kxxx bootloader之UDS bootloader
- .env.xxx 文件,加了常量,卻undefined
- Leetcode 96 différents arbres de recherche binaires
- Asp .NetCore 微信订阅号自动回复之文本篇
- 微信小程序缓存过期时间的相关设置(推荐)
- SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
- [es practice] safe operation mode on ES
猜你喜欢

TS initial use, TS type

Use vb Net to convert PNG pictures into icon type icon files

【.Net Core】程序相关各种全局文件

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

How to solve the image pop-up problem when pycharm calls Matplotlib to draw

ARP message header format and request flow

Review data desensitization system

Write some suggestions to current and future doctoral students to sort out and share

The essence of software architecture

S32Kxxx bootloader之UDS bootloader
随机推荐
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
Material design component - use bottomsheet to show extended content (I)
Use pair to do unordered_ Key value of map
如何提升数据质量
TS initial use, TS type
LeetCode中等题题分享(5)
.env.xxx 文件,加了常量,卻undefined
leetcode96不同的二叉搜索树
Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
Digital transformation has a long way to go, so how to take the key first step
Openwrt enable kV roaming
USB-IF协会与各种接口的由来
起床困难综合症(按位贪心)
Is there a piece of code that makes you convinced by human wisdom
RPA tutorial 01: Excel automation from introduction to practice
- Oui. Env. Fichier XXX, avec constante, mais non spécifié
Correlation - intra group correlation coefficient
Shell custom function
PWN attack and defense world cgpwn2
PostgreSQL notes (10) dynamically execute syntax parsing process