当前位置:网站首页>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
边栏推荐
- GCD summary
- IOS interview
- 解决控制文件全部损坏的异常
- How to automatically install homebrew in China (domestic address)
- 深开鸿:万物智联的大江上,升起一轮开源鸿蒙月
- 6 个超级良心的开源教程!
- Terminal output G_ Debug() information
- ACM SIGIR 2022 | 美团技术团队精选论文解读
- Achieve high throughput through Wi Fi 7 - insight into the next generation of Wi Fi physical layer
- Advanced C language: pointer (3)
猜你喜欢

2022年G2电站锅炉司炉考试题库模拟考试平台操作

Wechat applet development ④

Basic concept of MySQL database and deployment of MySQL version 8.0 (I)

With the "integration of driving and parking", freytek's high-performance domain controller leads the new track

Inheritance in swift

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

Wechat applet development ③

trivy【2】工具漏洞扫描

金仓数据库KingbaseES 客户端编程接口指南 - ODBC (2. 概述)

Runloop principle (I)
随机推荐
Development of small programs ②
MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
Swift type attribute and its attentions
Kotlin JVM annotation
Zero vision technology completed the pre-A round of financing and promoted the domestic replacement of intelligent driving platform software
Typescript prevents base classes from being instantiated
Anr questions often asked in Android interviews
MyCms 自媒体商城 v3.6 发布,兼容微擎应用开发(Laravel框架)
字节8年女测试总监工作感悟—写给想转行或即将进入测试行业的女生们...
Shenkaihong: on the river of Zhilian of all things, there is a bright moon of open source
网络流量监控工具iftop
GCD implementation and arc, blocks, GCD usage examples
Few people can really play in the "aftermarket" of the whole house intelligent fire collection
Elements in the middle (one article is enough)
Performance optimized APK slimming
金仓数据库KingbaseES 客户端编程接口指南 - ODBC特性支持约束
Research on cookies in WebView
In order for digital retail to continue to play its role, we need to give new connotation and significance to digital retail
Terminal output G_ Debug() information
Advanced C language: pointer (3)