当前位置:网站首页>MySQL - built in function

MySQL - built in function

2022-06-12 11:30:00 Lingling Ling

ni## One 、 Date function
 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here

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)

原网站

版权声明
本文为[Lingling Ling]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121025595130.html