当前位置:网站首页>MySQL - built in function
MySQL - built in function
2022-06-12 11:30:00 【Lingling Ling】
ni## One 、 Date function 
1.current_date(): Get date
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2022-06-07 |
+----------------+
1 row in set (0.00 sec)
2.current_time(): Get time, minutes, seconds
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 18:14:40 |
+----------------+
1 row in set (0.00 sec)
3.current_timestamp(): Get the timestamp
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2022-06-07 18:16:15 |
+---------------------+
1 row in set (0.00 sec)
4.date_add(): Date or time can be added
mysql> select date_add('2022-6-7',interval 10 day);
+--------------------------------------+
| date_add('2022-6-7',interval 10 day) |
+--------------------------------------+
| 2022-06-17 |
+--------------------------------------+
1 row in set (0.00 sec)
5.date_sub(): Subtract the date or time
mysql> select date_sub('2022-6-7',interval 10 day);
+--------------------------------------+
| date_sub('2022-6-7',interval 10 day) |
+--------------------------------------+
| 2022-05-28 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2022-6-7',interval -10 day);
+---------------------------------------+
| date_sub('2022-6-7',interval -10 day) |
+---------------------------------------+
| 2022-06-17 |
+---------------------------------------+
1 row in set (0.00 sec)
6.date_diff(): Statistical time difference
mysql> select datediff('2022-1-1','2022-1-10');
+----------------------------------+
| datediff('2022-1-1','2022-1-10') |
+----------------------------------+
| -9 |
+----------------------------------+
1 row in set (0.00 sec)
7.now(): Current date time ( Same result as timestamp )
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2022-06-07 18:26:22 |
+---------------------+
1 row in set (0.00 sec)
8. example
Tables created
mysql> desc msg;
+-----------+-------------+------+-----+--------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+--------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| nick_name | varchar(20) | NO | | NULL | |
| content | varchar(30) | YES | | No messages | |
| sendtime | datetime | YES | | NULL | |
+-----------+-------------+------+-----+--------------------+----------------+
4 rows in set (0.00 sec)
(1) Time to leave a message : current time , Show dates only , Show only time
mysql> select *from msg;
+----+--------------+--------------------------+---------------------+
| id | nick_name | content | sendtime |
+----+--------------+--------------------------+---------------------+
| 1 | Flying pigs | This child is so cute | 2022-06-08 12:46:42 |
| 2 | A dog climbing a tree | This child is so cute | 2022-06-08 12:47:39 |
+----+--------------+--------------------------+---------------------+
2 rows in set (0.00 sec)
mysql> select content,sendtime from msg;
+--------------------------+---------------------+
| content | sendtime |
+--------------------------+---------------------+
| This child is so cute | 2022-06-08 12:46:42 |
| This child is so cute | 2022-06-08 12:47:39 |
+--------------------------+---------------------+
2 rows in set (0.00 sec)
mysql> select content,date(sendtime) from msg;
+--------------------------+----------------+
| content | date(sendtime) |
+--------------------------+----------------+
| This child is so cute | 2022-06-08 |
| This child is so cute | 2022-06-08 |
+--------------------------+----------------+
2 rows in set (0.00 sec)
mysql> select content,time(sendtime) from msg;
+--------------------------+----------------+
| content | time(sendtime) |
+--------------------------+----------------+
| This child is so cute | 12:46:42 |
| This child is so cute | 12:47:39 |
+--------------------------+----------------+
2 rows in set (0.00 sec)
(2) Show Posts posted two minutes ago ( namely :now() <= sendtime + 2min)
mysql> select * from msg where date_add(sendtime,interval 2 minute) < now();
+----+--------------+--------------------------+---------------------+
| id | nick_name | content | sendtime |
+----+--------------+--------------------------+---------------------+
| 1 | Flying pigs | This child is so cute | 2022-06-08 12:46:42 |
| 2 | A dog climbing a tree | This child is so cute | 2022-06-08 12:47:39 |
| 3 | Peppa Pig | Nothing is difficult in the world | 2022-06-08 12:56:46 |
| 4 | Yellow duck | I'm just afraid that those who have a mind | 2022-06-08 12:57:07 |
+----+--------------+--------------------------+---------------------+
4 rows in set (0.00 sec)
(3) Posts posted within two minutes ( namely :now() <= sendtime + 2min)
mysql> select * from msg where date_add(sendtime,interval 2 minute) >= now();
+----+-----------+---------+---------------------+
| id | nick_name | content | sendtime |
+----+-----------+---------+---------------------+
| 5 | Yellow duck | come on. | 2022-06-08 13:02:26 |
| 6 | Yellow duck | Go ahead | 2022-06-08 13:02:37 |
+----+-----------+---------+---------------------+
2 rows in set (0.01 sec)
Two 、 String function

1.charset(str): Query the string character set of a column
mysql> select charset(nick_name) from msg;
+--------------------+
| charset(nick_name) |
+--------------------+
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
+--------------------+
6 rows in set (0.00 sec)
2.concat(): Connection string
Such as : Ask to show student Information in the table , Display format :“XXX My language is XXX branch , mathematics XXX branch , English XXX branch ”
mysql> select *from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 3 | Pig Wuneng | 176 | 98 | 90 |
| 4 | Cao mengde | 140 | 90 | 67 |
| 6 | king of Wu in the Three Kingdoms Era | 140 | 73 | 78 |
| 8 | The Monkey King | 87 | 98 | 95 |
+----+-----------+---------+------+---------+
4 rows in set (0.00 sec)
mysql> select concat(name,' My Chinese score is ',chinese,' branch ',' What's the math score ',math,' branch ',' The English score is ',english,' branch ') Overview of achievements from exam_result;
+---------------------------------------------------------------------------+
| Overview of achievements |
+---------------------------------------------------------------------------+
| Zhu Wuneng's Chinese achievement is 176 The score in mathematics is 98 The score in English is 90 branch |
| Cao mengde's Chinese achievement is 140 The score in mathematics is 90 The score in English is 67 branch |
| Sun Quan's Chinese achievement is 140 The score in mathematics is 73 The score in English is 78 branch |
| Monkey King's Chinese achievement is 87 The score in mathematics is 98 The score in English is 95 branch |
+---------------------------------------------------------------------------+
4 rows in set (0.00 sec)
3.length(str): Find the number of bytes occupied by the string
A Chinese character 3 byte
mysql> select name,length(name) from exam_result;
+-----------+--------------+
| name | length(name) |
+-----------+--------------+
| Pig Wuneng | 9 |
| Cao mengde | 9 |
| king of Wu in the Three Kingdoms Era | 6 |
| The Monkey King | 9 |
+-----------+--------------+
4 rows in set (0.00 sec)
4.replace(str,s1,s2):str Medium s1 Switch to s2
Such as : List all names in the table with S Of is replaced by ’ Shanghai ’
mysql> select name,replace(name,'S',' Shanghai ') from exam_result;
+-----------+----------------------------+
| name | replace(name,'S',' Shanghai ') |
+-----------+----------------------------+
| Pig Wuneng | Pig Wuneng |
| Cao mengde | Cao mengde |
| king of Wu in the Three Kingdoms Era | king of Wu in the Three Kingdoms Era |
| The Monkey King | The Monkey King |
| S Turn off | Shanghai Customs |
| lovely S | Lovely Shanghai |
+-----------+----------------------------+
6 rows in set (0.00 sec)
mysql> select replace('sxl','l',' Ling ');
+--------------------------+
| replace('sxl','l',' Ling ') |
+--------------------------+
| sx Ling |
+--------------------------+
1 row in set (0.00 sec)
5.substring(str,position,[ length ]): Intercepting string
mysql> select substring('abcdefg',3);
+------------------------+
| substring('abcdefg',3) |
+------------------------+
| cdefg |
+------------------------+
1 row in set (0.00 sec)
mysql> select substring('abcdefg',3,2);
+--------------------------+
| substring('abcdefg',3,2) |
+--------------------------+
| cd |
+--------------------------+
1 row in set (0.00 sec)
mysql> select substring(name,2,1),name from exam_result;
+---------------------+-----------+
| substring(name,2,1) | name |
+---------------------+-----------+
| enlightenment | Pig Wuneng |
| Meng | Cao mengde |
| power | king of Wu in the Three Kingdoms Era |
| enlightenment | The Monkey King |
| Turn off | S Turn off |
| Love | lovely S |
+---------------------+-----------+
6 rows in set (0.00 sec)
6.ucase(str): Convert to uppercase
mysql> select ucase('abcd');
+---------------+
| ucase('abcd') |
+---------------+
| ABCD |
+---------------+
1 row in set (0.00 sec)
7,lcase(str): Convert to lowercase
mysql> select lcase('ABCD');
+---------------+
| lcase('ABCD') |
+---------------+
| abcd |
+---------------+
1 row in set (0.00 sec)
Convert the first letter of a name to lowercase
8. Comprehensive use
Change the initial of your name to lowercase
step :(1) First separate the first letter from the following string
(2) Change the initial letter to lowercase
(3) Connection string
mysql> select substring(name,1,1),substring(name,2),name from exam_result;
+---------------------+-------------------+-----------+
| substring(name,1,1) | substring(name,2) | name |
+---------------------+-------------------+-----------+
| The pig | Enlightenment can | Pig Wuneng |
| Cao | Mendel | Cao mengde |
| Grandchildren | power | king of Wu in the Three Kingdoms Era |
| Grandchildren | The wu is empty | The Monkey King |
| S | Turn off | S Turn off |
| can | Love S | lovely S |
| M | ARY | MARY |
| B | OB | BOB |
+---------------------+-------------------+-----------+
8 rows in set (0.00 sec)
mysql> select lcase(substring(name,1,1)),substring(name,2),name from exam_result;
+----------------------------+-------------------+-----------+
| lcase(substring(name,1,1)) | substring(name,2) | name |
+----------------------------+-------------------+-----------+
| The pig | Enlightenment can | Pig Wuneng |
| Cao | Mendel | Cao mengde |
| Grandchildren | power | king of Wu in the Three Kingdoms Era |
| Grandchildren | The wu is empty | The Monkey King |
| s | Turn off | S Turn off |
| can | Love S | lovely S |
| m | ARY | MARY |
| b | OB | BOB |
+----------------------------+-------------------+-----------+
8 rows in set (0.00 sec)
mysql> select concat(lcase(substring(name,1,1)),substring(name,2)),name from exam_result;
+------------------------------------------------------+-----------+
| concat(lcase(substring(name,1,1)),substring(name,2)) | name |
+------------------------------------------------------+-----------+
| Pig Wuneng | Pig Wuneng |
| Cao mengde | Cao mengde |
| king of Wu in the Three Kingdoms Era | king of Wu in the Three Kingdoms Era |
| The Monkey King | The Monkey King |
| s Turn off | S Turn off |
| lovely S | lovely S |
| mARY | MARY |
| bOB | BOB |
+------------------------------------------------------+-----------+
8 rows in set (0.00 sec)
Capitalize the first letter of a name , The rest are in lowercase
step :(1) First separate the first letter from the following string
(2) Change the initial letter to uppercase , The following characters become lowercase
(3) Connection string
mysql> select substring(name,1,1),substring(name,2),name from exam_result;
+---------------------+-------------------+-----------+
| substring(name,1,1) | substring(name,2) | name |
+---------------------+-------------------+-----------+
| The pig | Enlightenment can | Pig Wuneng |
| Cao | Mendel | Cao mengde |
| Grandchildren | power | king of Wu in the Three Kingdoms Era |
| Grandchildren | The wu is empty | The Monkey King |
| S | Turn off | S Turn off |
| can | Love S | lovely S |
| M | ARY | MARY |
| B | OB | BOB |
| l | ily | lily |
| l | ucy | lucy |
+---------------------+-------------------+-----------+
10 rows in set (0.00 sec)
mysql> select ucase(substring(name,1,1)),substring(name,2),name from exam_result;
+----------------------------+-------------------+-----------+
| ucase(substring(name,1,1)) | substring(name,2) | name |
+----------------------------+-------------------+-----------+
| The pig | Enlightenment can | Pig Wuneng |
| Cao | Mendel | Cao mengde |
| Grandchildren | power | king of Wu in the Three Kingdoms Era |
| Grandchildren | The wu is empty | The Monkey King |
| S | Turn off | S Turn off |
| can | Love S | lovely S |
| M | ARY | MARY |
| B | OB | BOB |
| L | ily | lily |
| L | ucy | lucy |
+----------------------------+-------------------+-----------+
10 rows in set (0.00 sec)
mysql> select concat(ucase(substring(name,1,1)),substring(name,2)),name from exam_result;
+------------------------------------------------------+-----------+
| concat(ucase(substring(name,1,1)),substring(name,2)) | name |
+------------------------------------------------------+-----------+
| Pig Wuneng | Pig Wuneng |
| Cao mengde | Cao mengde |
| king of Wu in the Three Kingdoms Era | king of Wu in the Three Kingdoms Era |
| The Monkey King | The Monkey King |
| S Turn off | S Turn off |
| lovely S | lovely S |
| MARY | MARY |
| BOB | BOB |
| Lily | lily |
| Lucy | lucy |
+------------------------------------------------------+-----------+
10 rows in set (0.00 sec)
mysql> select concat(ucase(substring(name,1,1)),lcase(substring(name,2))),name from exam_rresult;
+-------------------------------------------------------------+-----------+
| concat(ucase(substring(name,1,1)),lcase(substring(name,2))) | name |
+-------------------------------------------------------------+-----------+
| Pig Wuneng | Pig Wuneng |
| Cao mengde | Cao mengde |
| king of Wu in the Three Kingdoms Era | king of Wu in the Three Kingdoms Era |
| The Monkey King | The Monkey King |
| S Turn off | S Turn off |
| lovely s | lovely S |
| Mary | MARY |
| Bob | BOB |
| Lily | lily |
| Lucy | lucy |
+-------------------------------------------------------------+-----------+
10 rows in set (0.00 sec)
9.instr(str,substr):substr stay str The first place in
mysql> select instr('sxlyiding','ly');
+-------------------------+
| instr('sxlyiding','ly') |
+-------------------------+
| 3 |
+-------------------------+
1 row in set (0.00 sec)
10,left(str,length):str Start from the left length Characters
mysql> select left(name,2),name from exam_result;
+--------------+-----------+
| left(name,2) | name |
+--------------+-----------+
| Pig enlightenment | Pig Wuneng |
| Cao meng | Cao mengde |
| king of Wu in the Three Kingdoms Era | king of Wu in the Three Kingdoms Era |
| Sun Wu | The Monkey King |
| S Turn off | S Turn off |
| lovely | lovely S |
| MA | MARY |
| BO | BOB |
| li | lily |
| lu | lucy |
+--------------+-----------+
10 rows in set (0.00 sec)
11.strcmp(str1,str2): Compare size character by character
str1 > str2 : return 1
str1 < str2: return -1
str1 == str2: return 0( Case insensitive )
mysql> select strcmp('ccc','bcc');
+---------------------+
| strcmp('ccc','bcc') |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.00 sec)
mysql> select strcmp('acc','bcc');
+---------------------+
| strcmp('acc','bcc') |
+---------------------+
| -1 |
+---------------------+
1 row in set (0.00 sec)
mysql> select strcmp('acc','Acc');
+---------------------+
| strcmp('acc','Acc') |
+---------------------+
| 0 |
+---------------------+
1 row in set (0.00 sec)
12.trim(str): Remove the leading or trailing spaces
ltrim(str): Remove left space
rtrim(str): Remove the right space
mysql> select trim(' sxl ');
+-----------------+
| trim(' sxl ') |
+-----------------+
| sxl |
+-----------------+
1 row in set (0.00 sec)
mysql> select rtrim(' a ') ret;
+-------+
| ret |
+-------+
| a |
+-------+
1 row in set (0.00 sec)
mysql> select ltrim(' a ') ret;
+------+
| ret |
+------+
| a |
+------+
1 row in set (0.00 sec)
mysql> select trim(' a ') ret;
+------+
| ret |
+------+
| a |
+------+
1 row in set (0.00 sec)
3、 ... and 、 Mathematical functions

1.abs(num): The absolute value
mysql> select abs(-123);
+-----------+
| abs(-123) |
+-----------+
| 123 |
+-----------+
1 row in set (0.00 sec)
2.ceiling(num): Rounding up
floor(num): Rounding down
mysql> select ceiling(12.3);
+---------------+
| ceiling(12.3) |
+---------------+
| 13 |
+---------------+
1 row in set (0.00 sec)
mysql> select floor(12.3);
+-------------+
| floor(12.3) |
+-------------+
| 12 |
+-------------+
1 row in set (0.00 sec)
3.:format(num,n): Keep the decimal places , Will round off
mysql> select format(2.14,1);
+----------------+
| format(2.14,1) |
+----------------+
| 2.1 |
+----------------+
1 row in set (0.00 sec)
mysql> select format(2.15,1);
+----------------+
| format(2.15,1) |
+----------------+
| 2.2 |
+----------------+
1 row in set (0.00 sec)
mysql> select format(2.5,0);
+---------------+
| format(2.5,0) |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec)
mysql> select format(-3.6,0);
+----------------+
| format(-3.6,0) |
+----------------+
| -4 |
+----------------+
1 row in set (0.00 sec)
4.bin(num): Decimal to binary
mysql> select bin(10);
+---------+
| bin(10) |
+---------+
| 1010 |
+---------+
1 row in set (0.00 sec)
5.hex(num): Convert to hex
mysql> select hex(15);
+---------+
| hex(15) |
+---------+
| F |
+---------+
1 row in set (0.00 sec)
6.mod(num1,num2): modulus
mysql> select mod(10,3);
+-----------+
| mod(10,3) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
Four 、 Other functions
1.user(): Query current user
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
2.md5(str): Summarize the string , Get one 32 Bit fixed length string ( Hidden text , application : The presentation of passwords )
mysql> select md5('abc');
+----------------------------------+
| md5('abc') |
+----------------------------------+
| 900150983cd24fb0d6963f7d28e17f72 |
+----------------------------------+
1 row in set (0.00 sec)
3.database(): View currently used databases
mysql> select database();
+------------+
| database() |
+------------+
| db1 |
+------------+
1 row in set (0.00 sec)
4.password():MySQL The database uses this function to encrypt users
mysql> select password('123');
+-------------------------------------------+
| password('123') |
+-------------------------------------------+
| *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-------------------------------------------+
1 row in set (0.00 sec)
5.ifnull(val1,val2): If val1 by null, return val2, Otherwise return to val1 Value
mysql> select ifnull('abc','123');
+---------------------+
| ifnull('abc','123') |
+---------------------+
| abc |
+---------------------+
1 row in set (0.00 sec)
mysql> select ifnull(null,'123');
+--------------------+
| ifnull(null,'123') |
+--------------------+
| 123 |
+--------------------+
1 row in set (0.00 sec)
边栏推荐
- Windows10安装mysql-8.0.28-winx64
- Thinking about the cooperation process of Network Library -- Some Thoughts on reading brpc
- Reading mysql45 lecture - self summary (part)
- MySQL lock leak detection and defect filling
- tensorflow 2. X multi classification confusion matrix and evaluation index calculation method (accuracy rate, recall rate, F1 score)
- DrQueueOnRails 集成 LDAP 验证
- Leetcode 162 Looking for peak value (June 11, 2022)
- Socket implements TCP communication flow
- SharDingJDBC-5.1.0按月水平分表+读写分离,自动创表、自动刷新节点表
- arm交叉编译链下载地址
猜你喜欢

21 reasons why you need social media QR code

Basic principle of Doppler effect

读mysql45讲-自我总结(部分)

【clickhouse专栏】基础数据类型说明

k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署

K58. Chapter 1 installing kubernetes V1.23 based on kubeadm -- cluster deployment

InfoQ geek media's 15th anniversary solicitation |position:fixed virtual button cannot take effect after being triggered. Problem analysis and Solution Exploration

AcWing 128. Editor (to effectively modify the specified position in the top stack)

MATLAB中stairs函数使用

Problems in cross validation code of 10% discount
随机推荐
InfoQ geek media's 15th anniversary solicitation |position:fixed virtual button cannot take effect after being triggered. Problem analysis and Solution Exploration
Go sends SMS based on Tencent cloud
NLP data set download address for naturallanguageprocessing
Redis keys in PHP
AcWing 1921. 重新排列奶牛(环图)
k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署
Windows10 install mysql-8.0.28-winx64
go基于阿里云实现发送短信
AcWing 1912. Odometer (enumeration)
Logrotate log rotation method create and copyruncate principles
信号继电器RXSF1-RK271018DC110V
Thinking about the cooperation process of Network Library -- Some Thoughts on reading brpc
Redis summary
CLJ3-100ALH30剩余电流继电器
Mysql45 lecture 01 | infrastructure: how is an SQL query executed?
【藍橋杯單片機 國賽 第十一届】
AcWing 1912. 里程表(枚举)
Differences among various cross compiling tools of arm
ReentrantLock源码分析
C# 36. DataGridView line number