当前位置:网站首页>Function application of MySQL
Function application of MySQL
2022-07-24 06:29:00 【zsm030616】
Catalog
One , Definition of function
1. similar Java Define methods
2. With parameters
3. Must have return value ( Line by line ) , a key :java Method can have no return value , The database function must have a return value
select max(score) from sc
4. Functions can be nested into sql In the sentence
notes : Stored procedures cannot
5. Functions can be nested round(avg)
Two , Common functions
(1) Date function
1. Date function
1) NOW(): Get the current date and time
select now()
2) DAY(): Get the day in the date
select date(NOW())
3) MONTH(): Gets the month in the date
select MONTH(NOW())
4) YEAR(): Gets the year in the date
select YEAR(NOW())
5) Date conversion function DATE_FORMAT()/STR_TO_DATE()
select NOW(),DATE_FORMAT(now(),'%Y year %m month %d Japan ')
DATE_FORMAT(): Convert date to string
STR_TO_DATE(): Convert string to date
Tips :< Case sensitive >
select STR_TO_DATE('2022-09-08','%Y-%m-%d')
design sketch 

Case 2
Check the age of each student , Only by year
select s.*,year(now())-YEAR(s.sage) from t_student s
According to the date of birth , Current month day < The date of birth is , Age minus one
select s.*,year(now())-YEAR(s.sage)+if(month(NOW())<MONTH(s.sage),-1,0) from t_student s
Check out the students who have birthdays this month
select *from t_student where month(now()) = month(sage)
select * from t_student
Check out the students who have their birthday next month ( Current month plus 1)
(1)date_add(NOW(), interval 1 MONTH)
select *from t_student where month(now())+1 = month(sage)
(2)select DATE_ADD(NOW(),interval 1 month)
select *from t_student where DATE_ADD(NOW(),interval 1 month) = month(sage)

3、 ... and , String function
Case study
1) UPPER()/LOWER(): Case to case
select UPPER('afsgafsas')
select LOWER('HJHKHKHKHK')
2) REPLACE(): Search and replace substrings in a string
SELECT uuid(),REPLACE((uuid()),'-','');
3) SUBSTRING(): The last substring starting at a position with a specific length
select SUBSTRING('xiong er is a xiong',1,5)
select SUBSTRING('xiong er is a xiong',5)
design sketch 
Case study 2
4) TRIM(): Remove the space before and after
5) LENGTH(): Get string length
select LENGTH('xiong er is a xiong'),LENGTH(TRIM( ' xiong er is a xiong '))
design sketch 
Four , Mathematical functions
Case study
3. Number function
1)FLOOR: Rounding down
2)CEIL: Rounding up
3)ROUND: rounding
select FLOOR(-3.14), CEIL(-3.14),CEIL(-3.14),CEIL(0.98);
design sketch 
5、 ... and , Judgment function
(1).IF(expr,v1,v2)
If expr yes TRUE Then return to v1, Otherwise return to v2 -----( Row column conversion )
demand : The extra linguistic scores of each student
Judge : If it is the current score, go to the current score , Use if not 0 Instead of
select s.sid,s.sname,
SUM(if(sc.cid='01',sc.score,0)) Chinese language and literature ,
SUM(if(sc.cid='02',sc.score,0)) mathematics ,
SUM(if(sc.cid='03',sc.score,0)) English
from
t_student s left JOIN t_score sc on s.sid=sc.sid GROUP BY s.sid,s.sname
design sketch 
(2)IFNULL(v1,v2)
If v1 Not for NULL, Then return to v1, Otherwise return to v2
grammar : CASE expr WHEN v1 THEN r1 [WHEN v2 THEN v2] [ELSE rn] END
If expr Is equal to a vn, Then return to the corresponding position THEN The latter result , If you don't want to wait with all the values , Then return to ELSE hinder rn
select s.sid,s.sname,
SUM(case when sc.cid = '01' THEN sc.score end) Chinese language and literature ,
SUM(case when sc.cid = '02' THEN sc.score end) mathematics ,
SUM(case when sc.cid = '03' THEN sc.score end) English
from
t_student s left JOIN t_score sc on s.sid=sc.sid GROUP BY s.sid,s.sname
design sketch 
6、 ... and 、 Merge (union)
(1) UNION: Merge all the query results , Then remove the same records
(2) UNION ALL: Merge all the query results , The same records will not be removed
Prerequisite : The number of columns in the result set is the same , The types of columns should be the same or compatible
Take only two pieces of data
select 1,'sa',18
union
select 2,'sa',18
union
select 2,'sa',18
Take all the data
select 1,'sa',18
union all
select 2,'sa',18
union all
select 2,'sa',18
边栏推荐
猜你喜欢
随机推荐
Li Kou 986. Intersection of interval lists
Wasm vs EVM, Boca's choice predicts the future of the public chain
Heap overflow of kernel PWN basic tutorial
第二周作业
Playing RTSP video stream on webpage
Website B video is embedded in the web page, and relevant controls are hidden
Flink function (1): rich function
Leetcode sword finger offer jz23: the entry node of the link in the linked list
Leetcode sword finger offer jz73 flip word sequence
Do not rent servers, build your own personal business website (3)
General paging 01
Leetcode sword finger offer jz25 merges two sorted linked lists
IP class notes (4)
Flink checkpoint configuration details
IP笔记(6)
Data set and pre training model
[no need for public IP] configure a fixed public TCP port address for remote desktop raspberry pie
Sword finger offer jz10 Fibonacci sequence
IP course (OSPF) comprehensive experiment
How to build a website full of ritual sense and publish it on the public website 2-2









