当前位置:网站首页>Flow control statement of SQL data analysis [if, case... When detailed]
Flow control statement of SQL data analysis [if, case... When detailed]
2022-07-02 00:05:00 【Chung, boundless Eagle】
List of articles
zero 、 Write it at the front
All the code in this article is in SQL ZOO platform Conduct , Data is also available under the platform world surface , All the codes have passed the test .
One 、if Single condition judgment statement
1、 Grammatical structure :if( Judge the condition , Establishment value , Invalid value )
select if(1=1,'Yes','No');
2、 It is judged that the soil area of China in Asia and Africa is larger than 200 Is a country with a large population ( The population is larger than 2 Billion )
select continent,name,if(population>200000000,' A large population ',' Small country ') Population size
from world
where continent in ('Asia','Africa') and area>2000000
order by continent
Execution results :
3、 Query land area is larger than 100 The overall economic level of million Asian countries ( Greater than 1 Trillion is a high economy )
select name,gdp,if(gdp>1000000000000,' High economy ',' Low economies ') as General economy
from world
where continent='Asia' and area>1000000

Two 、case…when Multi condition judgment
1、 Single condition (if…else effect )
Put the above if Modified into case…when:
# It is judged that the soil area of China in Asia and Africa is larger than 200 Is a country with a large population ( The population is larger than 2 Billion )
select continent,name,
CASE
WHEN population>200000000 THEN ' A large population '
ELSE ' Small country '
END AS Population size
from world
where continent in ('Asia','Africa') and area>2000000
order by continent
# Query land area is larger than 100 The overall economic level of million Asian countries ( Greater than 1 Trillion is a high economy )
select name,gdp,
CASE
when gdp>1000000000000 then ' High economy '
else ' Low economies '
END as General economy
from world
where continent='Asia' and area>1000000
2、 Multiple conditions (if…else if…else effect )
from gdp It is roughly judged that the land area is larger than 50 ten thousand , The population is larger than 2000 The state of ten thousand Asian and European countries :
select continent,name,population,gdp,
CASE
WHEN gdp>=3000000000000 THEN ' First class big country '
WHEN gdp>=2000000000000 and gdp<=3000000000000 THEN ' A second-class power '
WHEN gdp>=200000000000 and gdp<=1000000000000 THEN ' Third class big country '
ELSE ' Large developing countries '
END AS State of the country
from world
where continent in ('Asia','Europe') and area>500000 and population>20000000
order by continent

3、 Constant usage (switch…case effect )
case The field or expression to determine
when Constant 1 then The value to display 1 Or words 1
when Constant 2 then The value to display 2 Or words 2
…
else The value to display n Or words n;
for example : Make the population larger than 5000 The continent names of ten thousand countries are translated into the corresponding Chinese
select name,population,
CASE continent
WHEN 'Asia' THEN ' Asia '
WHEN 'Afica' THEN ' Africa '
WHEN 'South America' THEN ' South America '
WHEN 'North America' THEN ' In North America '
WHEN 'Europe' THEN ' The European '
ELSE ' Other continents '
END AS Continent
from world
where population>=50000000

3、 ... and 、ifnull Fill in empty values
Ifunull( Field name ,“ value 1”) If the content of this column is null, Then use ’' Fill in . If not null Words , Then the original value is displayed .
select id, ifnull('score',' miss an exam ') as fraction ;
If the score is null, It means that the student is absent .
边栏推荐
- Which app is better and more secure for stock mobile account opening
- Deep learning | three concepts: epoch, batch, iteration
- UDS bootloader of s32kxxx bootloader
- vs2015 AdminDeployment.xml
- Multi table operation - one to one, one to many and many to many
- 【模板】自适应辛普森积分
- Windows 7 install MySQL error: 1067
- 攻防演练复盘
- Shell process control
- .env.xxx 文件,加了常量,卻undefined
猜你喜欢
随机推荐
ADO. Net SqlCommand object
UDS bootloader of s32kxxx bootloader
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
Timer和ScheduledThreadPoolExecutor的区别
基于全志H3的QT5.12.9移植教程
在代码中使用SqlCommand对象
TS initial use, TS type
SQL数据分析之子查询的综合用法和案例题【耐心整理】
Three methods of finding inverse numbers
Door level modeling - after class exercises
攻防演练复盘
2021 robocom world robot developer competition - preliminary competition of undergraduate group
Shell process control
第六章 数据流建模
实例讲解将Graph Explorer搬上JupyterLab
ADO.NET 之sqlConnection 对象使用摘要
Chapter 6 data flow modeling
What is the purpose of ERP project implementation plan?
Windows 7 install MySQL error: 1067






![[es practice] safe operation mode on ES](/img/3f/fa28783770098ff10bffeccd64fe51.png)


