当前位置:网站首页>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 ~
边栏推荐
- VMware virtual machine bridging connectivity
- 【tinyriscv verilator】分支移植到正点原子达芬奇开发板
- What problems should be paid attention to in the serpentine wiring of PCB?
- 圖的存儲結構
- 良/恶性乳腺肿瘤预测(逻辑回归分类器)
- mysql读写分离配置
- 6G显卡显存不足出现CUDA Error:out of memory解决办法
- Typora 1.2.5等版本下载
- Google Earth Engine(GEE) 03-矢量数据类型
- Elk in Windows Environment - logstash+mysql (4)
猜你喜欢
Practice torch FX: pytorch based model optimization quantization artifact
[try to hack] kill evaluation
跨系统数据一致性问题解决方案汇总
[tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom
一文剖析C语言函数
What problems should be paid attention to in the serpentine wiring of PCB?
c语言之字符串数组
golang - new和make的区别
webService
Working at home is more tiring than going to work at the company?
随机推荐
matlab axis坐标轴相关设置详解
【数字IC/FPGA】检测最后一个匹配序列的位置
电子科大(申恒涛团队)&京东AI(梅涛团队)提出用于视频问答的结构化双流注意网络,性能SOTA!优于基于双视频表示的方法!
【PCL自学:Segmentation4】基于Min-Cut点云分割
Discuz taobaoke website template / Dean taobaoke shopping style commercial version template
How to solve the problem that the browser developed with CeF3 does not support flash
Swing UI——容器(一)
virtualbox扩展动态磁盘大小的坑
[electron] basic learning
Typora 1.2.5等版本下载
捷码赋能案例:湖南天辰产研实力迅速提升!实战玩转智慧楼宇/工地等项目
Online JSON to plaintext tool
小芯片chiplet技术杂谈
良/恶性乳腺肿瘤预测(逻辑回归分类器)
To build a "strong core" in Nansha, the first IC Nansha conference was held in Nansha
消除el-image图片周围间隙
Sentinel
c语言字符指针、字符串初始化问题
The file or assembly 'cefsharp.core.runtime.dll' or one of its dependencies could not be loaded. Is not a valid Win32 Application. (exception from hresult:0x800700c1)
ICML 2022: UFRGS |作为最优策略转移基础的乐观线性支持和后继特征