当前位置:网站首页>Zero foundation self-study SQL course | if function
Zero foundation self-study SQL course | if function
2022-06-28 11:06:00 【Game programming】
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 tJOIN Students sON t.Tid = s.TidGROUP BY t.Tname; Next time we will talk about CASE function .
Click on Focus on , Update the course and notify the first time ~
author : Meow Ningyi
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Gee: mcd64a1 based globfire daily fire data set
- MySQL installation configuration and solving the problem of forgetting root password when reinstalling MySQL
- Create ECS using API shortcut
- Metersphere implements UI automation elements that are not clickable (partially occluded)
- 毕业季,给初入社会的你一些建议
- 字符串 & 堆 & 方法区
- 远程登录sshd服务
- AQS understanding
- [function suggestion] select a space when multiple workspaces are started
- Katalon framework tests a web page operation example code
猜你喜欢

Installing MySQL database (CentOS) in Linux source code
![[practice] appium settings app is not running after 5000ms](/img/3c/5a7544561231a15a4bb5cfa9e102b3.png)
[practice] appium settings app is not running after 5000ms

Information hidden in the trend chart of Hong Kong London gold market

零基础自学SQL课程 | IF函数

Docker modifies the user name and password of MySQL

Dataease installation upgrade

MarkDown——基本使用语法

An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!

Word、PDF、TXT文件实现全文内容检索需要用什么方法?

培训通知|2022年境外中资企业机构及人员疫情防控和安全防范专题培训通知
随机推荐
元宇宙系统的发展与原理介绍
Secretary of the Ukrainian national security and National Defense Commission: will carry out precision strikes against targets in Russia
JS基础5
vsftpd服务的部署及优化
什么是DAPP系统发展与解析理解
Yann LeCun新论文:构建自动智能体之路
零基础自学SQL课程 | IF函数
Makefile简介
线程和线程池
Convert the file URL in the browser to a file stream
什么是主链系统?
Set up your own website (11)
Realization of a springboard machine
Threads and thread pools
Katalon框架测试web(二十)自定义关键字以及上传弹窗操作
JS foundation 1-js introduction and operator
How does ETF position affect spot gold price?
Compression and decompression
[QT] connect syntax reference implementation
Metersphere uses JS to refresh the current page