当前位置:网站首页>MySQL functions
MySQL functions
2022-07-28 23:35:00 【I love coriander TVT】
Catalog
Two 、 Process control functions
One 、 Common functions
1、 Character functions
| effect | function | result |
|---|---|---|
| Turn lowercase | LOWER('SQL Course') | sql course |
| Turn capitalization | UPPER('SQL Course') | SQL COURSE |
| Splicing | CONCAT('Hello', 'World') | HelloWorld |
| Intercept | SUBSTR('HelloWorld',1,5) | Hello |
| length | LENGTH('HelloWorld') | 10 |
| Character occurrence index value | INSTR('HelloWorld', 'W') | 6 |
| Second half of character interception | TRIM('H' FROM 'HelloWorld') | elloWorld |
| Character substitution | REPLACE('abcd','b','m') | amcd |
Character function cases :
1.length Gets the number of bytes of the parameter value
SELECT LENGTH('john');
SELECT LENGTH(' Little Zhu, little Zhu, little pig ');
SHOW VARIABLES LIKE '%char%'
2.concat String concatenation
SELECT CONCAT(last_name,'_',first_name) full name t_mysql_FROM employees;
3.upper、lower
SELECT UPPER('john');
SELECT LOWER('joHn');
Example : Capitalize your last name , Change the name to lowercase , Then joining together
SELECT CONCAT(UPPER(last_name),LOWER(first_name)) full name FROM t_mysql_employees;
4.substr、substring
Be careful : Index from 1 Start
Intercepts all characters following the specified index
SELECT SUBSTR('abcccccccccccc',7) out_put;
Intercepts the character with the specified length from the specified index
SELECT SUBSTR(' Xiao Zhu Xiao Zhu is chubby ',1,3) out_put;
Case study : The first character of a name is capitalized , The other characters are lowercase and then use _ Splicing , Show it
SELECT CONCAT(UPPER(SUBSTR(last_name,1,1)),'_',LOWER(SUBSTR(last_name,2))) out_put
FROM t_mysql_employees;5.instr Returns the index of the first occurrence of the substring , If we can't find a way back 0
SELECT INSTR('aaabbbb','aaa') AS out_put;
#6.trim
SELECT LENGTH(TRIM(' Zhu Jiayin ')) AS out_put;
SELECT TRIM('aa' FROM 'aaaaaaaaa zhu aaaaaaaaaaaa Good news aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') AS out_put;
7.lpad Fill the specified length with the specified characters
SELECT LPAD(' Xiao Zhu ',2,'*') AS out_put;
8.rpad Fill the specified length with the specified characters
SELECT RPAD(' Xiao Zhu ',12,'ab') AS out_put;
9.replace Replace
SELECT REPLACE(' Xiao Zhu loves to sleep ',' Xiao Zhu ',' Two bears ') AS out_put;2、 Number function
| effect | function | result |
|---|---|---|
| rounding | ROUND(45.926, 2) | 45.93 |
| truncation | TRUNC(45.926, 2) | 45.92 |
| Seeking remainder | MOD(1600, 300) | 100 |
Mathematical function cases :
round rounding
SELECT ROUND(-1.55);
SELECT ROUND(1.567,2);
#ceil Rounding up , return >= The smallest integer of this parameter
SELECT CEIL(-1.02);
#floor Rounding down , return <= The maximum integer of the parameter
SELECT FLOOR(-9.99);
#truncate truncation
SELECT TRUNCATE(1.69999,1);3、 Date function
| effect | function | result |
|---|---|---|
| Get current date | now() | |
| Convert the character of date format to the date of specified format | STR_TO_DATE('9-13-1999','%m-%d-%Y') | 1999-09-13 |
| Convert date to character | DATE_FORMAT(‘2018/6/6’,‘%Y year %m month %d Japan ’) | 2018 year 06 month 06 Japan |
Date function case :
now Returns the current system date + Time
SELECT NOW();
curdate Returns the current system date , Not including time
SELECT CURDATE();
curtime Return current time , No date included
SELECT CURTIME();
You can get the specified part , year 、 month 、 Japan 、 Hours 、 minute 、 second
SELECT YEAR(NOW()) year ;
SELECT YEAR('1998-1-1') year ;
SELECT YEAR(hiredate) year FROM t_mysql_employees;
SELECT MONTH(NOW()) month ;
SELECT MONTHNAME(NOW()) month ;
str_to_date Converts a character to a date in a specified format
SELECT STR_TO_DATE('1998-3-2','%Y-%c-%d') AS out_put;
date_format Convert date to character
SELECT DATE_FORMAT(NOW(),'%y year %m month %d Japan ') AS out_put;4、 Other functions
SELECT VERSION();
SELECT DATABASE();
SELECT USER();
Two 、 Process control functions
1.if function : if else The effect of
SELECT IF(10<5,' Big ',' Small ');
SELECT last_name,commission_pct,IF(commission_pct IS NULL,' No bonus , ha-ha ',' Bonus , Hee hee ') remarks
FROM t_mysql_employees;
2.case The use of function one : switch case The effect of
java in
switch( Variable or expression ){
case Constant 1: sentence 1;break;
...
default: sentence n;break;
}
mysql in
case The field or expression to determine
when Constant 1 then The value to display 1 Or words 1;
when Constant 2 then The value to display 2 Or words 2;
...
else The value to display n Or words n;
end
3、 ... and 、 Merge (union)
1、union: Merge all the query results together , Then remove the same records
2、union All: Merge all the query results together , The same records will not be removed
Prerequisite : The number of columns in the result set is the same , The types of columns should also be the same or compatible
Application scenarios : Project report module , Merge data
边栏推荐
- WebView whitelist
- String string
- 2022年G2电站锅炉司炉考试题库模拟考试平台操作
- Achieve high throughput through Wi Fi 7 - insight into the next generation of Wi Fi physical layer
- How to embed AI digital human function in VR panorama? Create a cloud experience
- Binary search tree
- Advanced C language: pointer (2)
- View APK signature
- 字节8年女测试总监工作感悟—写给想转行或即将进入测试行业的女生们...
- [mongodb] basic use of mongodb database, special cases, and the installation and creation process of mongoose (including the installation of mongoose fixed version)
猜你喜欢

Inheritance in swift

Price for volume has encountered "six consecutive declines" in sales. Can Volvo, which is no longer safe, turn around?

参加竞赛同学们的留言 : 第十七届的记忆

The front mounted ADAS camera in parking increased by 54.15% year-on-year, with TOP10 suppliers taking the lead

WebView optimization

互动滑轨屏在展厅中应用的制作步骤

「行泊一体」放量,福瑞泰克高性能域控制器领跑新赛道

Advanced C language: pointer (3)

Applet, JS, transfer object jump transfer parameter problem

Wechat applet development ④
随机推荐
High quality subroutine 3 - a good name
如何在VR全景中嵌入AI数字人功能?打造云端体验感
Typescript类方法this指针绑定
22牛客多校day1 J - Serval and Essay 启发式合并
【数据挖掘工程师-笔试】2022年大华股份
trivy【2】工具漏洞扫描
GCD implementation and arc, blocks, GCD usage examples
Programmer growth Chapter 30: artifact of identifying true and false needs
解决控制文件全部损坏的异常
The development mode of digital retail dominated by traffic is only the beginning
Development of small programs ②
金仓数据库KingbaseES 客户端编程接口指南 - ODBC (2. 概述)
Solve the problem of using anonymous users in pod due to the failure of attaching ciphertext token files for serviceaccount user authentication
Performance optimized APK slimming
Mycms we media mall V3.6 release, compatible with micro engine application development (laravel framework)
22 Niuke multi school Day1 I - Introduction to chiitoitsu DP
Rhce第二天
IOS interview
金仓数据库 KingbaseES V8.3至V8.6迁移最佳实践(2. KingbaseES V8.3和 V8.6 兼容性)
集火全屋智能“后装市场”,真正玩得转的没几个