当前位置:网站首页>Tutorial on principles and applications of database system (053) -- MySQL query (XV): usage of character functions
Tutorial on principles and applications of database system (053) -- MySQL query (XV): usage of character functions
2022-07-26 01:09:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (053)—— MySQL Inquire about ( 15、 ... and ): Usage of character function
Catalog
- Database system principle and Application Tutorial (053)—— MySQL Inquire about ( 15、 ... and ): Usage of character function
- One 、 Create data table
- Two 、 Character processing functions
- 3、 ... and 、 Evaluate string length function
- Four 、 Connection string function
- 5、 ... and 、 String replacement function
- 6、 ... and 、 Case conversion function
- 7、 ... and 、 Intercept substring function
- 8、 ... and 、 Find substring function
- Nine 、 Other functions
Using functions when querying can construct more flexible query conditions ,MySQL Provides functions for processing strings .
One 、 Create data table
The data table used for query is as follows :
-- Table structure
mysql> desc student;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| s_id | char(5) | NO | PRI | NULL | |
| s_name | char(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
| addr | varchar(100) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
6 rows in set (0.10 sec)
-- Table data
mysql> select * from student;
+-------+-----------+--------+---------------------+-------------+-----------+
| s_id | s_name | gender | birth | phone | addr |
+-------+-----------+--------+---------------------+-------------+-----------+
| S2011 | Zhang Xiaogang | male | 1999-12-03 00:00:00 | 13163735775 | Xinyang City |
| S2012 | Liu Xiaoqing | Woman | 1999-10-11 00:00:00 | 13603732255 | Xinxiang City |
| S2013 | Cao mengde | male | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city |
| S2014 | Liu Yan | Woman | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city |
| S2015 | Liu Yan | Woman | 1999-07-06 00:00:00 | 13813735225 | Xinyang City |
| S2016 | Liu Ruofei | Woman | 2000-08-31 00:00:00 | 13683735533 | Kaifeng City |
| S2021 | Dong Wenhua | Woman | 2000-07-30 00:00:00 | 13533735564 | Kaifeng City |
| S2022 | Zhou Huajian | male | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
| S2023 | trump | male | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City |
| S2024 | Obama | male | 2000-10-17 00:00:00 | 13843735885 | Xinyang City |
| S2025 | Zhou Jianhua | male | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City |
| S2026 | Zhang Xueyou | male | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
| S2031 | Lee Myung Bak | Woman | 1999-10-26 00:00:00 | 13643732222 | Zhengzhou city |
| S2032 | Vinci | male | 1999-12-31 00:00:00 | 13043731234 | Zhengzhou city |
+-------+-----------+--------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)
Two 、 Character processing functions
1、ASCII() function
Returns the... Of a character ASCII code , If the parameter is a string , Returns the first character ASCII code .
The syntax is as follows :
-- The parameters of the function are characters
-- If the parameter is a string , Returns the... Of the first character in the string ASCII code
ASCII(str)
2、CHAR() function
The multiple ASCII Code is converted to the corresponding character , And connect it into a string .
The syntax is as follows :
-- Parameters are several representations ASCII Integer of code
-- Return from n1,n2,… Of ASCII Code corresponding to the character string
CHAR(n1, n2, ...)
for example :
mysql> select ascii('a') lower,char(ascii('a')-32) upper;
+-------+-------+
| lower | upper |
+-------+-------+
| 97 | A |
+-------+-------+
1 row in set (0.00 sec)
mysql> select ascii('b') lower,char(ascii('b')-32) upper;
+-------+-------+
| lower | upper |
+-------+-------+
| 98 | B |
+-------+-------+
1 row in set (0.00 sec)
3、 ... and 、 Evaluate string length function
1、CHAR_LENGTH() function
Returns the number of characters contained in the string ( Both Chinese and Western characters are counted as one character ).
The syntax is as follows :
-- The parameter of the function is a string
-- Returns the number of characters contained in the string , Including half width characters and full width characters
CHAR_LENGTH(str)
for example :
/* select s_id, char_length(s_id), s_name, char_length(s_name) from student where addr=' Zhengzhou city '; */
mysql> select s_id, char_length(s_id), s_name, char_length(s_name)
-> from student where addr=' Zhengzhou city ';
+-------+-------------------+-----------+---------------------+
| s_id | char_length(s_id) | s_name | char_length(s_name) |
+-------+-------------------+-----------+---------------------+
| S2013 | 5 | Cao mengde | 3 |
| S2014 | 5 | Liu Yan | 2 |
| S2022 | 5 | Zhou Huajian | 3 |
| S2026 | 5 | Zhang Xueyou | 3 |
| S2031 | 5 | Lee Myung Bak | 3 |
| S2032 | 5 | Vinci | 3 |
+-------+-------------------+-----------+---------------------+
6 rows in set (0.12 sec)
2、LENGTH() function
Returns the length of a string , In bytes .
The syntax is as follows :
-- The parameter of the function is a string
-- Returns the length of the string ( The unit is byte )
-- The length of multibyte characters depends on the character set used . such as utf8mb4 A Chinese character in the character set is 4 Bytes ,utf8 A Chinese character in the character set is 3 Bytes , and latin1 A Chinese character in the character set is 2 byte .
LENGTH(str)
for example :
/* select s_id, length(s_id), s_name, length(s_name) from student where addr=' Zhengzhou city '; */
mysql> select s_id, length(s_id), s_name, length(s_name)
-> from student where addr=' Zhengzhou city ';
+-------+--------------+-----------+----------------+
| s_id | length(s_id) | s_name | length(s_name) |
+-------+--------------+-----------+----------------+
| S2013 | 5 | Cao mengde | 9 |
| S2014 | 5 | Liu Yan | 6 |
| S2022 | 5 | Zhou Huajian | 9 |
| S2026 | 5 | Zhang Xueyou | 9 |
| S2031 | 5 | Lee Myung Bak | 9 |
| S2032 | 5 | Vinci | 9 |
+-------+--------------+-----------+----------------+
6 rows in set (0.02 sec)
-- student The character set used in the table is utf8, A Chinese character is 3 Bytes
mysql> show create table student\G
*************************** 1. row ***************************
Table: student
Create Table: CREATE TABLE `student` (
`s_id` char(5) NOT NULL,
`s_name` char(20) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`birth` datetime DEFAULT NULL,
`phone` char(20) DEFAULT NULL,
`addr` varchar(100) DEFAULT NULL,
PRIMARY KEY (`s_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.04 sec)
Four 、 Connection string function
1、CONCAT() function
Connect two or more strings into one string .
The syntax is as follows :
-- The parameters of the function are several strings
-- Connect the strings contained in the parameters into a string , Then return
CONCAT(str1,str2,...)
for example :
/* select concat(s_name, ':', phone, ':', addr) name_phone_addr from student where addr=' Zhengzhou city '; */
mysql> select concat(s_name, ':', phone, ':', addr) name_phone_addr
-> from student where addr=' Zhengzhou city ';
+---------------------------------+
| name_phone_addr |
+---------------------------------+
| Cao mengde :13853735522: Zhengzhou city |
| Liu Yan :13623735335: Zhengzhou city |
| Zhou Huajian :13243735578: Zhengzhou city |
| Zhang Xueyou :13743735566: Zhengzhou city |
| Lee Myung Bak :13643732222: Zhengzhou city |
| Vinci :13043731234: Zhengzhou city |
+---------------------------------+
6 rows in set (0.00 sec)
2、CONCAT_WS() function
Used for the connection of multiple strings , and CONCAT() Function , This function can specify the separator between multiple strings .
The syntax is as follows :
-- The first parameter is the separator between strings when connecting strings
-- Connect the strings contained in the parameters into a string , And separated by the specified separator
CONCAT_WS(separator,st1,st2,...)
for example :
/* select concat_ws(':', s_name, phone, addr) name_phone_addr from student where addr=' Zhengzhou city '; */
mysql> select concat_ws(':', s_name, phone, addr) name_phone_addr
-> from student where addr=' Zhengzhou city ';
+---------------------------------+
| name_phone_addr |
+---------------------------------+
| Cao mengde :13853735522: Zhengzhou city |
| Liu Yan :13623735335: Zhengzhou city |
| Zhou Huajian :13243735578: Zhengzhou city |
| Zhang Xueyou :13743735566: Zhengzhou city |
| Lee Myung Bak :13643732222: Zhengzhou city |
| Vinci :13043731234: Zhengzhou city |
+---------------------------------+
6 rows in set (0.00 sec)
3、GROUP_CONCAT() function
Connect all the data contained in a column , You can specify the separator between each data ( Default to comma ), It is often used in grouping queries .
The syntax is as follows :
-- This function processes a column in the table , Connect all the data contained in this column
-- This function is generally used for grouping queries , Its usage is similar to aggregate function
-- Connect all the data contained in a column , from separator Parameter specifies the separator , If omitted separator, The default separator is comma
-- distinct Parameters are used to remove duplicate data
-- order by Parameter specifies to sort by a field in the table before connecting
GROUP_CONCAT([distinct] fieldname [order by Sort field asc|desc] separator ' Separator ')
for example :
(1) Do not specify separator , Default to comma
mysql> select addr, group_concat(s_name) name from student group by addr;
+-----------+----------------------------------------------------------+
| addr | name |
+-----------+----------------------------------------------------------+
| Xinyang City | Zhang Xiaogang , Liu Yan , Obama |
| Kaifeng City | Liu Ruofei , Dong Wenhua , Zhou Jianhua |
| Xinxiang City | Liu Xiaoqing , trump |
| Zhengzhou city | Cao mengde , Liu Yan , Zhou Huajian , Zhang Xueyou , Lee Myung Bak , Vinci |
+-----------+----------------------------------------------------------+
4 rows in set (0.08 sec)
(2) Specify the separator
mysql> select addr, group_concat(s_name separator '||') name from student group by addr;
+-----------+---------------------------------------------------------------+
| addr | name |
+-----------+---------------------------------------------------------------+
| Xinyang City | Zhang Xiaogang || Liu Yan || Obama |
| Kaifeng City | Liu Ruofei || Dong Wenhua || Zhou Jianhua |
| Xinxiang City | Liu Xiaoqing || trump |
| Zhengzhou city | Cao mengde || Liu Yan || Zhou Huajian || Zhang Xueyou || Lee Myung Bak || Vinci |
+-----------+---------------------------------------------------------------+
4 rows in set (0.00 sec)
(3) Remove duplicate data when connecting
mysql> select group_concat(addr) addr from student;
+------------------------------------------------------------------------------------+
| addr
+------------------------------------------------------------------------------------+
| Xinyang City , Xinxiang City , Zhengzhou city , Zhengzhou city , Xinyang City , Kaifeng City , Kaifeng City , Zhengzhou city , Xinxiang City , Xinyang City , Kaifeng City , Zhengzhou city , Zhengzhou city , Zhengzhou city
+------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select group_concat(distinct addr) addr from student;
+-----------------------------------------+
| addr |
+-----------------------------------------+
| Xinyang City , Kaifeng City , Xinxiang City , Zhengzhou city |
+-----------------------------------------+
1 row in set (0.02 sec)
(4) Sort by a column before connecting
mysql> select s_id, s_name, addr from student where addr=' Zhengzhou city ' order by s_id desc;
+-------+-----------+-----------+
| s_id | s_name | addr |
+-------+-----------+-----------+
| S2032 | Vinci | Zhengzhou city |
| S2031 | Lee Myung Bak | Zhengzhou city |
| S2026 | Zhang Xueyou | Zhengzhou city |
| S2022 | Zhou Huajian | Zhengzhou city |
| S2014 | Liu Yan | Zhengzhou city |
| S2013 | Cao mengde | Zhengzhou city |
+-------+-----------+-----------+
6 rows in set (0.01 sec)
mysql> select addr, group_concat(s_name) name from student where addr=' Zhengzhou city ';
+-----------+----------------------------------------------------------+
| addr | name |
+-----------+----------------------------------------------------------+
| Zhengzhou city | Cao mengde , Liu Yan , Zhou Huajian , Zhang Xueyou , Lee Myung Bak , Vinci |
+-----------+----------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select addr, group_concat(s_name order by s_id desc) name from student where addr=' Zhengzhou city ';
+-----------+----------------------------------------------------------+
| addr | name |
+-----------+----------------------------------------------------------+
| Zhengzhou city | Vinci , Lee Myung Bak , Zhang Xueyou , Zhou Huajian , Liu Yan , Cao mengde |
+-----------+----------------------------------------------------------+
1 row in set (0.00 sec)
5、 ... and 、 String replacement function
1、INSERT() function
Replace some characters in a string with other characters .
The syntax is as follows :
-- Put the string str from pos The position begins len Replace characters with instr character string , And return the replaced string
-- If pos Exceeds string length , Then no replacement will be made , Return the original string directly .
INSERT(str,pos,len,instr)
for example : hold phone Medium 4-7 Replace characters with 【*】
mysql> select s_id,s_name,phone,insert(phone,4,4,'****') phone11 from student;
+-------+-----------+-------------+-------------+
| s_id | s_name | phone | phone11 |
+-------+-----------+-------------+-------------+
| S2011 | Zhang Xiaogang | 13163735775 | 131****5775 |
| S2012 | Liu Xiaoqing | 13603732255 | 136****2255 |
| S2013 | Cao mengde | 13853735522 | 138****5522 |
| S2014 | Liu Yan | 13623735335 | 136****5335 |
| S2015 | Liu Yan | 13813735225 | 138****5225 |
| S2016 | Liu Ruofei | 13683735533 | 136****5533 |
| S2021 | Dong Wenhua | 13533735564 | 135****5564 |
| S2022 | Zhou Huajian | 13243735578 | 132****5578 |
| S2023 | trump | 13343735588 | 133****5588 |
| S2024 | Obama | 13843735885 | 138****5885 |
| S2025 | Zhou Jianhua | 13788736655 | 137****6655 |
| S2026 | Zhang Xueyou | 13743735566 | 137****5566 |
| S2031 | Lee Myung Bak | 13643732222 | 136****2222 |
| S2032 | Vinci | 13043731234 | 130****1234 |
+-------+-----------+-------------+-------------+
14 rows in set (0.01 sec)
2、REPLACE() function
Replace some characters in a string with other characters .
The syntax is as follows :
-- hold str1 Medium str2 Substring is replaced with str3 Substring
-- If the position of the string to be replaced is determined , I don't know the content , Use INSERT() function
-- If the content of the string to be replaced is determined , The location is uncertain , Use REPLACE() function
REPLACE(str1,str2,str3)
for example : hold s_name Medium 【 Hua 】 Replace the word with Pinyin 【HUA】
mysql> select s_id, s_name, replace(s_name, ' Hua ', 'HUA') name2 from student;
+-------+-----------+-----------+
| s_id | s_name | name2 |
+-------+-----------+-----------+
| S2011 | Zhang Xiaogang | Zhang Xiaogang |
| S2012 | Liu Xiaoqing | Liu Xiaoqing |
| S2013 | Cao mengde | Cao mengde |
| S2014 | Liu Yan | Liu Yan |
| S2015 | Liu Yan | Liu Yan |
| S2016 | Liu Ruofei | Liu Ruofei |
| S2021 | Dong Wenhua | Dong Wenhua |
| S2022 | Zhou Huajian | Zhou HUA build |
| S2023 | trump | trump |
| S2024 | Obama | Obama |
| S2025 | Zhou Jianhua | Zhou Jian HUA |
| S2026 | Zhang Xueyou | Zhang Xueyou |
| S2031 | Lee Myung Bak | Lee Myung Bak |
| S2032 | Vinci | Vinci |
+-------+-----------+-----------+
14 rows in set (0.03 sec)
6、 ... and 、 Case conversion function
MySQL Four case conversion functions are provided .
The syntax is as follows :
-- UCASE and UPPER You can convert the letters in the string to uppercase letters
-- LCASE and LOWER You can convert the letters in the string to lowercase letters .
-- Non alphabetic does not convert
LCASE(str)
UCASE(str)
LOWER(str)
UPPER(str)
for example :
mysql> select @str := 'I am a Student123.';
+------------------------------+
| @str := 'I am a Student123.' |
+------------------------------+
| I am a Student123. |
+------------------------------+
1 row in set (0.00 sec)
mysql> select UCASE(@str), UPPER(@str) from dual;
+--------------------+--------------------+
| UCASE(@str) | UPPER(@str) |
+--------------------+--------------------+
| I AM A STUDENT123. | I AM A STUDENT123. |
+--------------------+--------------------+
1 row in set (0.00 sec)
mysql> select LCASE(@str), LOWER(@str) from dual;
+--------------------+--------------------+
| LCASE(@str) | LOWER(@str) |
+--------------------+--------------------+
| i am a student123. | i am a student123. |
+--------------------+--------------------+
1 row in set (0.00 sec)
7、 ... and 、 Intercept substring function
This kind of function can intercept a substring from a string .
The syntax is as follows :
LEFT(str,n) -- Intercept from the front of the string n Character and return
RIGHT(str,n) -- Intercept from the rightmost end of the string n Character and return
SUBSTR(str,m,n) -- From m Character start , Intercept n Character and return
SUBSTRING(str,m,n) -- From m Character start , Intercept n Character and return
for example :
(1) Show phone The last four
mysql> select s_id,s_name,phone,right(phone,4) phone22 from student;
+-------+-----------+-------------+---------+
| s_id | s_name | phone | phone22 |
+-------+-----------+-------------+---------+
| S2011 | Zhang Xiaogang | 13163735775 | 5775 |
| S2012 | Liu Xiaoqing | 13603732255 | 2255 |
| S2013 | Cao mengde | 13853735522 | 5522 |
| S2014 | Liu Yan | 13623735335 | 5335 |
| S2015 | Liu Yan | 13813735225 | 5225 |
| S2016 | Liu Ruofei | 13683735533 | 5533 |
| S2021 | Dong Wenhua | 13533735564 | 5564 |
| S2022 | Zhou Huajian | 13243735578 | 5578 |
| S2023 | trump | 13343735588 | 5588 |
| S2024 | Obama | 13843735885 | 5885 |
| S2025 | Zhou Jianhua | 13788736655 | 6655 |
| S2026 | Zhang Xueyou | 13743735566 | 5566 |
| S2031 | Lee Myung Bak | 13643732222 | 2222 |
| S2032 | Vinci | 13043731234 | 1234 |
+-------+-----------+-------------+---------+
14 rows in set (0.00 sec)
(2) Ask the students 【 What's your last name 】
mysql> select s_id,s_name,left(s_name,1) name22 from student;
+-------+-----------+--------+
| s_id | s_name | name22 |
+-------+-----------+--------+
| S2011 | Zhang Xiaogang | Zhang |
| S2012 | Liu Xiaoqing | Liu |
| S2013 | Cao mengde | Cao |
| S2014 | Liu Yan | Liu |
| S2015 | Liu Yan | Liu |
| S2016 | Liu Ruofei | Liu |
| S2021 | Dong Wenhua | Dong |
| S2022 | Zhou Huajian | Zhou |
| S2023 | trump | , |
| S2024 | Obama | Mr. |
| S2025 | Zhou Jianhua | Zhou |
| S2026 | Zhang Xueyou | Zhang |
| S2031 | Lee Myung Bak | Li |
| S2032 | Vinci | reach |
+-------+-----------+--------+
14 rows in set (0.00 sec)
(3) Show phone Of the 4 To the first 7 position
mysql> select s_id,s_name,phone,substr(phone,4,4) phone22 from student;
+-------+-----------+-------------+---------+
| s_id | s_name | phone | phone22 |
+-------+-----------+-------------+---------+
| S2011 | Zhang Xiaogang | 13163735775 | 6373 |
| S2012 | Liu Xiaoqing | 13603732255 | 0373 |
| S2013 | Cao mengde | 13853735522 | 5373 |
| S2014 | Liu Yan | 13623735335 | 2373 |
| S2015 | Liu Yan | 13813735225 | 1373 |
| S2016 | Liu Ruofei | 13683735533 | 8373 |
| S2021 | Dong Wenhua | 13533735564 | 3373 |
| S2022 | Zhou Huajian | 13243735578 | 4373 |
| S2023 | trump | 13343735588 | 4373 |
| S2024 | Obama | 13843735885 | 4373 |
| S2025 | Zhou Jianhua | 13788736655 | 8873 |
| S2026 | Zhang Xueyou | 13743735566 | 4373 |
| S2031 | Lee Myung Bak | 13643732222 | 4373 |
| S2032 | Vinci | 13043731234 | 4373 |
+-------+-----------+-------------+---------+
14 rows in set (0.00 sec)
8、 ... and 、 Find substring function
1、LOCATE() function
Find where a string appears in another string .
The syntax is as follows :
-- If str1 yes str2 Substring of , Then return the position where the substring first appears , Otherwise return to 0
LOCATE(str1, str2)
for example : Find a phone number that contains 5566 Student information
-- Use wildcards
mysql> select * from student where phone like '%5566%';
+-------+-----------+--------+---------------------+-------------+-----------+
| s_id | s_name | gender | birth | phone | addr |
+-------+-----------+--------+---------------------+-------------+-----------+
| S2026 | Zhang Xueyou | male | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
+-------+-----------+--------+---------------------+-------------+-----------+
1 row in set (0.00 sec)
-- Use locate() function
mysql> select *,locate('5566',phone) position from student where locate('5566',phone)>0;
+-------+-----------+--------+---------------------+-------------+-----------+----------+
| s_id | s_name | gender | birth | phone | addr | position |
+-------+-----------+--------+---------------------+-------------+-----------+----------+
| S2026 | Zhang Xueyou | male | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city | 8 |
+-------+-----------+--------+---------------------+-------------+-----------+----------+
1 row in set (0.02 sec)
2、POSITION() function
Find where a string appears in another string .
The syntax is as follows :
-- If str1 yes str2 Substring of , Then return the position where the substring first appears , Otherwise return to 0
-- LOCATE Functions and POSTION The function uses the same , The difference is LOCATE The two parameters of the function are separated by commas ,POSITION The two parameters of the function are in separate
POSITION(str1 in str2)
for example : Find a phone number that contains 55 Student information
-- Use wildcards
mysql> select * from student where phone like '%55%';
+-------+-----------+--------+---------------------+-------------+-----------+
| s_id | s_name | gender | birth | phone | addr |
+-------+-----------+--------+---------------------+-------------+-----------+
| S2012 | Liu Xiaoqing | Woman | 1999-10-11 00:00:00 | 13603732255 | Xinxiang City |
| S2013 | Cao mengde | male | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city |
| S2016 | Liu Ruofei | Woman | 2000-08-31 00:00:00 | 13683735533 | Kaifeng City |
| S2021 | Dong Wenhua | Woman | 2000-07-30 00:00:00 | 13533735564 | Kaifeng City |
| S2022 | Zhou Huajian | male | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
| S2023 | trump | male | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City |
| S2025 | Zhou Jianhua | male | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City |
| S2026 | Zhang Xueyou | male | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
+-------+-----------+--------+---------------------+-------------+-----------+
8 rows in set (0.00 sec)
-- Use position() function
mysql> select *,position('55' in phone) position from student where position('55' in phone)>0;
+-------+-----------+--------+---------------------+-------------+-----------+----------+
| s_id | s_name | gender | birth | phone | addr | position |
+-------+-----------+--------+---------------------+-------------+-----------+----------+
| S2012 | Liu Xiaoqing | Woman | 1999-10-11 00:00:00 | 13603732255 | Xinxiang City | 10 |
| S2013 | Cao mengde | male | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city | 8 |
| S2016 | Liu Ruofei | Woman | 2000-08-31 00:00:00 | 13683735533 | Kaifeng City | 8 |
| S2021 | Dong Wenhua | Woman | 2000-07-30 00:00:00 | 13533735564 | Kaifeng City | 8 |
| S2022 | Zhou Huajian | male | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city | 8 |
| S2023 | trump | male | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City | 8 |
| S2025 | Zhou Jianhua | male | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City | 10 |
| S2026 | Zhang Xueyou | male | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city | 8 |
+-------+-----------+--------+---------------------+-------------+-----------+----------+
8 rows in set (0.00 sec)
Nine 、 Other functions
1、BIN() function
Convert an integer into a binary number , The type of the returned result is string .
The syntax is as follows :
-- The argument is an integer , If the parameter is decimal , Then it will be rounded before processing
-- Put the number n Convert to binary form , The return value is a function of 0 and 1 Composed string
BIN(n)
for example :
mysql> select bin(101),bin(2.89) from dual;
+----------+-----------+
| bin(101) | bin(2.89) |
+----------+-----------+
| 1100101 | 10 |
+----------+-----------+
1 row in set (0.03 sec)
2、FORMAT() function
Round a value and return , The type of the returned result is string .
The syntax is as follows :
-- For the value x Perform rounding operation , Retain d Is a decimal , The return value is a string
-- The function and ROUND() Function similar to , Only the types of return values are different
ORMAT(x,d)
for example :
mysql> select format(25.1258,2),format(1254.2,-1) from dual;
+-------------------+-------------------+
| format(25.1258,2) | format(1254.2,-1) |
+-------------------+-------------------+
| 25.13 | 1,254 |
+-------------------+-------------------+
1 row in set (0.02 sec)
3、REPEAT() function
Generate a string repeated by n A string composed of times .
The syntax is as follows :
-- Generate a n individual str Connected string
REPEAT(str,n)
for example :
mysql> select repeat('abc',4),repeat(' Wang Peng ',3) from dual;
+-----------------+--------------------+
| repeat('abc',4) | repeat(' Wang Peng ',3) |
+-----------------+--------------------+
| abcabcabcabc | Wang Peng, Wang Peng, Wang Peng |
+-----------------+--------------------+
1 row in set (0.00 sec)
mysql> select concat(s_id,repeat('=',4),'>',s_name) from student;
+---------------------------------------+
| concat(s_id,repeat('=',4),'>',s_name) |
+---------------------------------------+
| S2011====> Zhang Xiaogang |
| S2012====> Liu Xiaoqing |
| S2013====> Cao mengde |
| S2014====> Liu Yan |
| S2015====> Liu Yan |
| S2016====> Liu Ruofei |
| S2021====> Dong Wenhua |
| S2022====> Zhou Huajian |
| S2023====> trump |
| S2024====> Obama |
| S2025====> Zhou Jianhua |
| S2026====> Zhang Xueyou |
| S2031====> Lee Myung Bak |
| S2032====> Vinci |
+---------------------------------------+
14 rows in set (0.00 sec)
4、SPACE() function
Generate a string consisting of several spaces .
The syntax is as follows :
-- Return from n A string of spaces
SPACE(n)
for example :
mysql> select concat(s_id,space(6),s_name) from student;
+------------------------------+
| concat(s_id,space(6),s_name) |
+------------------------------+
| S2011 Zhang Xiaogang |
| S2012 Liu Xiaoqing |
| S2013 Cao mengde |
| S2014 Liu Yan |
| S2015 Liu Yan |
| S2016 Liu Ruofei |
| S2021 Dong Wenhua |
| S2022 Zhou Huajian |
| S2023 trump |
| S2024 Obama |
| S2025 Zhou Jianhua |
| S2026 Zhang Xueyou |
| S2031 Lee Myung Bak |
| S2032 Vinci |
+------------------------------+
14 rows in set (0.00 sec)
5、REVERSE() function
Flip the contents of a string .
The syntax is as follows :
-- return str The flip string of
REVERSE(str)
for example :
mysql> select s_id, s_name, reverse(s_name) from student;
+-------+-----------+-----------------+
| s_id | s_name | reverse(s_name) |
+-------+-----------+-----------------+
| S2011 | Zhang Xiaogang | Just Xiao Zhang |
| S2012 | Liu Xiaoqing | Qing Xiaoliu |
| S2013 | Cao mengde | De mengcao |
| S2014 | Liu Yan | Yanliu |
| S2015 | Liu Yan | Yanliu |
| S2016 | Liu Ruofei | Fei Ruo Liu |
| S2021 | Dong Wenhua | Hua Wendong |
| S2022 | Zhou Huajian | Jianhua week |
| S2023 | trump | Prant |
| S2024 | Obama | Mabao |
| S2025 | Zhou Jianhua | Hua Jianzhou |
| S2026 | Zhang Xueyou | There is Zhang |
| S2031 | Lee Myung Bak | Bo Ming Li |
| S2032 | Vinci | Chifanda |
+-------+-----------+-----------------+
14 rows in set (0.00 sec)
6、STRCMP() function
Compare the size of two strings .
The syntax is as follows :
-- According to the rules of comparison , If the string str1 Greater than str2, return 1
-- If the string str1 Less than str2, return -1
-- If the string str1 and str2 Exactly the same , Then return to 0
STRCMP(str1,str2)
for example :
mysql> select strcmp('abc','abc'),strcmp('abc','bcd'),strcmp('abc','123') from dual;
+---------------------+---------------------+---------------------+
| strcmp('abc','abc') | strcmp('abc','bcd') | strcmp('abc','123') |
+---------------------+---------------------+---------------------+
| 0 | -1 | 1 |
+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)
7、SUBSTRING_INDEX() function
The syntax is as follows :
-- If n Greater than 0, Returns the number from the left n individual delimiter All contents to the left of the character represented
-- If n Less than 0, Return the number from the right n individual delimiter All contents to the right of the character represented
SUBSTRING_INDEX(str,delimiter,n)
for example : There is the following table pic.
/* create table pic(id int primary key,name char(20),file varchar(200)); insert into pic values(1,'flower','c:\\mydoc\\picture\\flower.jpg'); insert into pic values(2,'sun','d:\\camera\\picture\\sun.png'); insert into pic values(3,'spring','d:\\camera\\pic\\spring.psd'); insert into pic values(4,'moon','e:\\history\\2011-3-22\\moon.jpg'); insert into pic values(5,'school','e:\\history\\2020-12-3\\school.jpg'); */
mysql> select * from pic;
+----+--------+---------------------------------+
| id | name | file |
+----+--------+---------------------------------+
| 1 | flower | c:\mydoc\picture\flower.jpg |
| 2 | sun | d:\camera\picture\sun.png |
| 3 | spring | d:\camera\pic\spring.psd |
| 4 | moon | e:\history\2011-3-22\moon.jpg |
| 5 | school | e:\history\2020-12-3\school.jpg |
+----+--------+---------------------------------+
5 rows in set (0.00 sec)
(1) The extension of the file in the query table
mysql> select *,substring_index(file,'.',-1) from pic;
+----+--------+---------------------------------+------------------------------+
| id | name | file | substring_index(file,'.',-1) |
+----+--------+---------------------------------+------------------------------+
| 1 | flower | c:\mydoc\picture\flower.jpg | jpg |
| 2 | sun | d:\camera\picture\sun.png | png |
| 3 | spring | d:\camera\pic\spring.psd | psd |
| 4 | moon | e:\history\2011-3-22\moon.jpg | jpg |
| 5 | school | e:\history\2020-12-3\school.jpg | jpg |
+----+--------+---------------------------------+------------------------------+
5 rows in set (0.00 sec)
(2) Query the file name in the table ( No path )
mysql> select *,substring_index(file,'\\',-1) from pic;
+----+--------+---------------------------------+-------------------------------+
| id | name | file | substring_index(file,'\\',-1) |
+----+--------+---------------------------------+-------------------------------+
| 1 | flower | c:\mydoc\picture\flower.jpg | flower.jpg |
| 2 | sun | d:\camera\picture\sun.png | sun.png |
| 3 | spring | d:\camera\pic\spring.psd | spring.psd |
| 4 | moon | e:\history\2011-3-22\moon.jpg | moon.jpg |
| 5 | school | e:\history\2020-12-3\school.jpg | school.jpg |
+----+--------+---------------------------------+-------------------------------+
5 rows in set (0.00 sec)
(3) Query the file path in the table
/* select *, left(file,char_length(file)-char_length(substring_index(file,'\\',-1))) path from pic; */
mysql> select *,
-> left(file,char_length(file)-char_length(substring_index(file,'\\',-1))) path
-> from pic;
+----+--------+---------------------------------+-----------------------+
| id | name | file | path |
+----+--------+---------------------------------+-----------------------+
| 1 | flower | c:\mydoc\picture\flower.jpg | c:\mydoc\picture\ |
| 2 | sun | d:\camera\picture\sun.png | d:\camera\picture\ |
| 3 | spring | d:\camera\pic\spring.psd | d:\camera\pic\ |
| 4 | moon | e:\history\2011-3-22\moon.jpg | e:\history\2011-3-22\ |
| 5 | school | e:\history\2020-12-3\school.jpg | e:\history\2020-12-3\ |
+----+--------+---------------------------------+-----------------------+
5 rows in set (0.00 sec)
(4) Query the drive letter of the file in the table
mysql> select *,substring_index(file,'\\',1) from pic;
+----+--------+---------------------------------+------------------------------+
| id | name | file | substring_index(file,'\\',1) |
+----+--------+---------------------------------+------------------------------+
| 1 | flower | c:\mydoc\picture\flower.jpg | c: |
| 2 | sun | d:\camera\picture\sun.png | d: |
| 3 | spring | d:\camera\pic\spring.psd | d: |
| 4 | moon | e:\history\2011-3-22\moon.jpg | e: |
| 5 | school | e:\history\2020-12-3\school.jpg | e: |
+----+--------+---------------------------------+------------------------------+
5 rows in set (0.00 sec)
8、LPAD() And RPAD() function
LPAD() And RPAD() Function fills several characters in the front or back of a string , Make the string reach the specified length .
The syntax is as follows :
-- Put the string str use padstr The string is filled to a length of len, And returns a length of len String
-- LPAD function str String right justified ,padstr Fill in from the left
-- RPAD function str String left justified ,padstr Fill from the right end
LPAD(str, len, padstr)
RPAD(str, len, padstr)
for example :
mysql> select id,concat(name,'||',file) from pic;
+----+-----------------------------------------+
| id | concat(name,'||',file) |
+----+-----------------------------------------+
| 1 | flower||c:\mydoc\picture\flower.jpg |
| 2 | sun||d:\camera\picture\sun.png |
| 3 | spring||d:\camera\pic\spring.psd |
| 4 | moon||e:\history\2011-3-22\moon.jpg |
| 5 | school||e:\history\2020-12-3\school.jpg |
+----+-----------------------------------------+
5 rows in set (0.00 sec)
-- rpad() function : Align left , Fill in space on the right
mysql> select id,concat(rpad(name,8,' '),'||',file) from pic;
+----+-------------------------------------------+
| id | concat(rpad(name,8,' '),'||',file) |
+----+-------------------------------------------+
| 1 | flower ||c:\mydoc\picture\flower.jpg |
| 2 | sun ||d:\camera\picture\sun.png |
| 3 | spring ||d:\camera\pic\spring.psd |
| 4 | moon ||e:\history\2011-3-22\moon.jpg |
| 5 | school ||e:\history\2020-12-3\school.jpg |
+----+-------------------------------------------+
5 rows in set (0.00 sec)
-- lpad() function : Right alignment , Fill in the space on the left
mysql> select id,concat(rpad(name,8,' '),'||',lpad(file,35,' ')) from pic;
+----+-------------------------------------------------+
| id | concat(rpad(name,8,' '),'||',lpad(file,35,' ')) |
+----+-------------------------------------------------+
| 1 | flower || c:\mydoc\picture\flower.jpg |
| 2 | sun || d:\camera\picture\sun.png |
| 3 | spring || d:\camera\pic\spring.psd |
| 4 | moon || e:\history\2011-3-22\moon.jpg |
| 5 | school || e:\history\2020-12-3\school.jpg |
+----+-------------------------------------------------+
5 rows in set (0.00 sec)
边栏推荐
- How to switch IP and move bricks with mobile game simulator
- Lock upgrade: no lock, bias lock, lightweight lock, heavyweight lock
- 【RTOS训练营】程序框架、预习、课后作业和晚课提问
- EasyCVR页面添加Loading加载效果的实现过程
- Upload local file trial version using SAP ui5 fileuploader control
- Spine_附件皮肤
- Django database addition, deletion, modification and query
- Using questpdf operation to generate PDF is faster and more efficient!
- 《自然语言处理实战入门》深度学习基础 ---- attention 注意力机制 ,Transformer 深度解析与学习材料汇总
- Four common simple and effective methods for changing IP addresses
猜你喜欢

Embedded development: tips and tricks -- seven tips for designing powerful boot loader

How can I become an irreplaceable programmer?

Set set learning

How to choose social e-commerce model in the early stage? Taishan crowdfunding
![[RTOS training camp] continue the program framework, tick interrupt supplement, preview, after-school homework and evening class questions](/img/79/27e4709dd6381c8887e12a1a8da257.jpg)
[RTOS training camp] continue the program framework, tick interrupt supplement, preview, after-school homework and evening class questions

Codeforces Round #810 (Div. 2)A~C

【RTOS训练营】课程学习方法和结构体知识复习 + 链表知识

Working principle of ZK rollups

Lock upgrade: no lock, bias lock, lightweight lock, heavyweight lock
![[Jizhong] July 16, 2022 1432. Oil pipeline](/img/60/55a7e35cd067948598332d08eccfb1.jpg)
[Jizhong] July 16, 2022 1432. Oil pipeline
随机推荐
Four common simple and effective methods for changing IP addresses
Subarray with 19 and K
[RTOS training camp] course learning methods and C language knowledge (pointer, structure, function pointer, linked list) and student questions
Game thinking 17: Road finding engine recast and detour learning II: recast navigation grid generation process and limitations
ORACLE——iSupplier 门户开票错误
Tencent employees' salary: the real 985 graduation salary, do you think I can be saved? Netizen: daily salary?
什么是信息化?什么是数字化?这两者有什么联系和区别?
Spine_附件皮肤
【软件开发规范二】《禁止项开发规范》
Upload local file trial version using SAP ui5 fileuploader control
【Code】剑指offer 03数组中重复的数字
Small sample learning data set
[Code] refers to the repeated number in the offer 03 array
The ultra comprehensive open source WinForm UI library meets all your desktop development needs!
数据库系统原理与应用教程(053)—— MySQL 查询(十五):字符型函数的用法
【软件开发规范三】【软件版本命名规范】
【RTOS训练营】晚课学员问题
How to copy and paste QT? (QClipboard)
Download exclusively | Alibaba cloud maxcompute questions and answers to unlock SaaS mode cloud data warehouse in this electronic manual!
How does the proxy IP server ensure its information security in the network