当前位置:网站首页>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 ~
边栏推荐
- Excel print settings public header
- 【Try to Hack】veil-evasion免杀
- C# Winform 读取Resources图片
- Discuz taobaoke website template / Dean taobaoke shopping style commercial version template
- The latest cloud development wechat balance charger special effect applet source code
- 【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer
- EasyCVR平台路由日志功能的技术实现过程【附代码】
- [learn FPGA programming from scratch -48]: Vision - development and application of intelligent sensors
- Discuz small fish game wind shadow legend business gbk+utf8 version template /dz game website template
- apipost脚本使用讲解一~全局变量
猜你喜欢

Golang - the difference between new and make

ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码

golang - new和make的区别

Feign implements path escape through custom annotations

Practice torch FX: pytorch based model optimization quantization artifact

Teach you how to transplant tinyriscv to FPGA

webserver流程图——搞懂webserver各模块间调用关系

What if Fiddler fails to listen to the interface

Online JSON to plaintext tool

UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation!
随机推荐
【Try to Hack】veil-evasion免杀
[sword finger offer] 48 Longest substring without duplicate characters
【数字IC/FPGA】检测最后一个匹配序列的位置
实践torch.fx:基于Pytorch的模型优化量化神器
Feign implements path escape through custom annotations
matlab axis坐标轴相关设置详解
const关键字及其作用(用法),C语言const详解
小芯片chiplet技术杂谈
Online JSON to plaintext tool
One step forward is excellent, one step backward is ignorant
发射,接收天线方向图
webserver流程图——搞懂webserver各模块间调用关系
[electron] basic learning
圖的存儲結構
浙江大学课程攻略共享计划
What problems should be paid attention to in the serpentine wiring of PCB?
MSP430F5529 单片机 读取 GY-906 红外温度传感器
mysql 字符集
[electron] basic learning
C# Winform 读取Resources图片