当前位置:网站首页>Zero foundation self-study SQL course | if function
Zero foundation self-study SQL course | if function
2022-06-27 23:41:00 【Meow Ningyi】
Hello everyone , I'm Ning Yi .
Today is our first 22 course :IF function .
If you have learned other programming languages , Should be right IF Functions are familiar . Used according to conditions , Return different values .
Basic grammar :
IF( Conditional expression , value 1, value 2)If the conditional expression is True, Return value 1, by False, Return value 2.
The return value can be any value , such as : The number 、 Text 、 date 、 Null value NULL、 Mathematical expression 、 Functions, etc .
1、 IF function
example : stay Students In the table , take 1995 Students born in and before are divided into 1 class ,1995 Students born after are divided into 2 class , Finally, it shows Sname,Sage, Class 3 Column .

Sample results :

SELECT
Sname,
Sage,
IF(
YEAR(Sage) <=1995, "1 class ","2 class "
) AS " In class "
FROM Students;2、IF Nested function
When the classification is greater than two , Can be IF Function nesting implementation .
example : stay Students In the table , Student number Sid Less than or equal to 3 Of the students , Belong to 1 class ;Sid by 4 To 6 Of the students , Belong to 2 class ;Sid Greater than or equal to 7 Students of 3 class . Finally, it shows Sname,Sage, Class 3 Column .
SELECT
Sid,
Sname,
IF(Sid<=3,"1 class ",
IF(Sid>=7,'3 class ','2 class ')
) AS " In class "
FROM Students
Nested functions still seem a bit cumbersome , If there are more than two categories , It can be used CASE Keywords replace IF Nested function , In the next class, we will explain in detail .
3、IF+ Aggregate functions
IF Functions are also often used in conjunction with aggregate functions , Let's take a look at an example .
example : aggregate Teacher Table and Students, Count the number of students that the head teacher brings , Greater than or equal to 5 Display of “5 People and above ”, The number of people is less than 5 Display of “5 People here ”.

Sample results :

SELECT
t.Tname,
COUNT(*) AS " Number of students ",
IF(COUNT(*)>=5,"5 People and above ","5 People here ") AS " classification "
FROM Teachers t
JOIN Students s
ON t.Tid = s.Tid
GROUP BY t.Tname;Next time we will talk about CASE function .
Click on Focus on , Update the course and notify the first time ~
边栏推荐
- EasyCVR平台路由日志功能的技术实现过程【附代码】
- To build a "strong core" in Nansha, the first IC Nansha conference was held in Nansha
- 撰写外文时怎样引用中文文献?
- golang - new和make的区别
- One step forward is excellent, one step backward is ignorant
- Introduction to quantitative trading
- 最新云开发微信余额充电器特效小程序源码
- 【Try to Hack】veil-evasion免杀
- Discuz淘宝客网站模板/迪恩淘宝客购物风格商业版模板
- 发射,接收天线方向图
猜你喜欢
随机推荐
Halcon's region: features of multiple regions (6)
Vivado FFT IP的使用说明
C# Winform 读取Resources图片
【数字IC/FPGA】检测最后一个匹配序列的位置
【PCL自学:Segmentation3】基于PCL的点云分割:区域增长分割
思源笔记订阅停止直接删云端数据嘛?
How to solve the problem that the browser developed with CeF3 does not support flash
ClickOnce error deploying ClickOnce application - the reference in the manifest does not match the identity of the downloaded assembly
seata
Course strategy sharing plan of Zhejiang University
c语言字符指针、字符串初始化问题
SQL中IS NOT NULL与!=NULL的区别
[Blue Bridge Cup training 100 questions] scratch digital calculation Blue Bridge Cup competition special prediction programming question collective training simulation exercise question No. 16
C language character pointer and string initialization
Discuz小鱼游戏风影传说商业GBK+UTF8版模板/DZ游戏网站模板
在线JSON转PlainText工具
通过 MQTT 检测对象和传输图像
golang使用mongo-driver操作——查(基础)
手把手教你移植 tinyriscv 到FPGA上
pytorch实现kaggle猫狗识别





![[try to hack] kill evaluation](/img/93/e623e25dc4dec1f656227c7651577e.png)



