当前位置:网站首页>MySQL functions
MySQL functions
2022-07-06 00:00:00 【lion tow】
Catalog
3、 ... and 、 Conditional judgment function
5、 ... and 、 Aggregate functions
One 、 Date function
1) NOW(): Get the current date and time
2) DAY(): Get the day in the date
3) MONTH(): Gets the month in the date
4) YEAR(): Gets the year in the date
5) Date conversion function DATE_FORMAT()/STR_TO_DATE()
① NOW(): Get the current date and time
select now();
②DAY(): Get the day in the date
select day(now());
③MONTH(): Gets the month in the date
select MONTH(now());
④ YEAR(): Gets the year in the date
select year(now());
⑤ Date conversion function DATE_FORMAT()/STR_TO_DATE()
DATE_FORMAT(): Convert date to string
select now(),DATE_FORMAT(now(),'%Y year %m month %d Japan ')
STR_TO_DATE(): Convert string to date
select STR_TO_DATE('2022-12-12','%Y-%m-%d')
Case a : Check the age of each student , Only by year
select s.*,YEAR(now())-year(s.sage) from t_student s
Case 2 : 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
Case three : Check out the students who have birthdays this month ( If not, it will be displayed as NULL)
select * from t_student where MONTH(now()) = MONTH(sage);
Case four : Check out the students who have their birthday next month ( The two methods ):
select * from t_student where MONTH(now())+1 = MONTH(sage);
select * from t_student where date_add(NOW(), interval 1 MONTH) = MONTH(sage);
Two 、 String function
2. String function
1) UPPER()/LOWER(): Case to case
2) REPLACE(): Search and replace substrings in a string
3) SUBSTRING(): The last substring starting at a position with a specific length
4) TRIM(): Remove the space before and after
5) LENGTH(): Get string length
6) CONCAT(): Merge string functions , The result is the string generated by the connection parameter , Parameters can be one or more
3. Number function
1)FLOOR: Rounding down
2)CEIL: Rounding up
3)ROUND: rounding
① UPPER() / LOWER(): Case to case
select upper(' beautiful girl ')
select lower(' GOOD MAN')
②REPLACE(): Search and replace substrings in a string
select 'zhangqiag',REPLACE('zhang-qiag','-',' ');
③ SUBSTRING(): The last substring starting at a position with a specific length
select SUBSTRING('zhang qiang na li',2,5)// From the beginning to the end
select SUBSTRING('zhang qiang na li',5)// From the first few , Until the end
④ TRIM(): Remove the space before and after
⑤ LENGTH(): Get string length
select LENGTH(' zhang qiang na li'),LENGTH(trim(' zhang qiang na li '))
⑥CONCAT(): Merge string functions , The return result is the string generated by the connection parameter , Parameters can be one or more
select CONCAT('zhangqiang111','nali222');
⑦FLOOR: Rounding down
⑧CEIL: Rounding up
⑨ROUND: rounding
select FLOOR(-3.14),FLOOR(3.14), CEIL(-3.14), CEIL(3.14),round(-3.14),round(3.14)
3、 ... and 、 Conditional judgment function
1)IF(expr,v1,v2) If expr yes TRUE Then return to v1, Otherwise return to v2
2)IFNULL(v1,v2) If v1 Not for NULL, Then return to v1, Otherwise return to v2
3)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
①IF(expr,v1,v2) If expr yes TRUE Then return to v1, Otherwise return to v2
②IFNULL(v1,v2) If v1 Not for NULL, Then return to v1, Otherwise return to v2
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
③case ...when....
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
Four 、 Merge functions
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
①UNION: Merge the query results , Then remove the same records
select 1,'zs',18
union
select 2,'ls',22
union
select 2,'ls',22
②UNION ALL: Merge all the query results , The same records will not be removed
select 1,'zs',18
union all
select 2,'ls',22
union all
select 2,'ls',22
5、 ... and 、 Aggregate functions
Aggregate functions (SUM/COUNT/AVG/MAX/MIN)
SUM(): Sum up . Often with GROUP BY Use it together , It can also be used alone
AVG(): averaging . Often with GROUP BY Use it together , It can also be used alone
MAX(): For maximum . Often with GROUP BY Use it together , It can also be used alone
MIN(): For the minimum . Often with GROUP BY Use it together , It can also be used alone
COUNT(): Count the number of records . Often with GROUP BY Use it together , It can also be used alone
I shared it last time, and I'll mention it this time !
边栏推荐
- 行列式学习笔记(一)
- 7.5 装饰器
- C reflection and type
- Hardware and interface learning summary
- Chapter 16 oauth2authorizationrequestredirectwebfilter source code analysis
- Fiddler Everywhere 3.2.1 Crack
- Effet Doppler (déplacement de fréquence Doppler)
- CloudCompare&PCL 点云随机添加噪声
- [Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
- "14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
猜你喜欢
Determinant learning notes (I)
亲测可用fiddler手机抓包配置代理后没有网络
20220703 week race: number of people who know the secret - dynamic rules (problem solution)
Configuring OSPF GR features for Huawei devices
Senparc.Weixin.Sample.MP源码剖析
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
零犀科技携手集智俱乐部:“因果派”论坛成功举办,“因果革命”带来下一代可信AI
云呐|固定资产管理系统功能包括哪些?
【在线聊天】原来微信小程序也能回复Facebook主页消息!
硬件及接口学习总结
随机推荐
What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
GFS Distributed File System
20220703 week race: number of people who know the secret - dynamic rules (problem solution)
CloudCompare&PCL 点云随机添加噪声
Huawei equipment configuration ospf-bgp linkage
Permission problem: source bash_ profile permission denied
【GYM 102832H】【模板】Combination Lock(二分图博弈)
Spire. PDF for NET 8.7.2
2022.7.5-----leetcode. seven hundred and twenty-nine
Tools to improve work efficiency: the idea of SQL batch generation tools
openssl-1.0.2k版本升级openssl-1.1.1p
Zhongjun group launched electronic contracts to accelerate the digital development of real estate enterprises
亲测可用fiddler手机抓包配置代理后没有网络
mysql-全局锁和表锁
[day39 literature extensive reading] a Bayesian perspective on magnetic estimation
跟着CTF-wiki学pwn——ret2libc1
14 MySQL-视图
【EF Core】EF Core与C# 数据类型映射关系
Biased sample variance, unbiased sample variance
行列式学习笔记(一)