当前位置:网站首页>Tutorial on principles and applications of database system (042) -- MySQL query (4): using wildcards to construct query conditions
Tutorial on principles and applications of database system (042) -- MySQL query (4): using wildcards to construct query conditions
2022-07-24 00:49:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (042)—— MySQL Inquire about ( Four ): Use wildcards to construct query conditions
Catalog
When querying data , Wildcards can replace one of the specified positions ( Underline :_) Or more characters ( Percent sign :%).
One 、MySQL The wildcard
MySQL You can use wildcards as shown below :
| wildcard | describe |
|---|---|
| Percent sign ( % ) | Expressed in percent sign (%) The position can have any character , The content of characters is unlimited |
| Underline ( _ ) | Indicates in the underline ( _ ) The position has only one character , The content of characters is unlimited |
explain :
(1) If wildcards are used in the condition , The comparison operator must use LIKE, They can't be used =、>、< Equal operator . If the comparison operator is not used LIKE, be MySQL Think the percent sign and underscore are ordinary characters , Not wildcards .
(2) If wildcards are used in the condition , When querying, if the wildcard is placed on the left , Index will not be used .
Two 、 wildcard : Percent sign ( % )
When querying, it means that there can be any (0 - n individual ) character , The content of characters is unlimited .
for example :
(1) Check the last name 【 Liu 】 Student information .
mysql> select * from student where s_name like ' Liu %';
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2012 | Liu Xiaoqing | 1999-10-11 00:00:00 | 13603732255 | Xinxiang City |
| S2014 | Liu Yan | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city |
| S2015 | Liu Yan | 1999-07-06 00:00:00 | 13813735225 | Xinyang City |
| S2016 | Liu Ruofei | 2000-08-31 00:00:00 | 13683735533 | Kaifeng City |
+-------+-----------+---------------------+-------------+-----------+
4 rows in set (0.00 sec)
-- This query uses an index idx_name
mysql> explain select * from student where s_name like ' Liu %'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: student
partitions: NULL
type: range
possible_keys: idx_name
key: idx_name
key_len: 61
ref: NULL
rows: 4
filtered: 100.00
Extra: Using index condition
1 row in set, 1 warning (0.00 sec)
-- Wildcard usage =, No query results , Unless the student's name is 【 Liu %】 Will be queried .
mysql> select * from student where s_name = ' Liu %';
Empty set (0.00 sec)
(2) The last word of the query is 【 Just. 】 Student information .
mysql> select * from student where s_name like '% Just. ';
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2011 | Zhang Xiaogang | 1999-12-03 00:00:00 | 13163735775 | Xinyang City |
+-------+-----------+---------------------+-------------+-----------+
1 row in set (0.00 sec)
-- This query will not use indexes
mysql> explain select * from student where s_name like '% Just. '\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: student
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 14
filtered: 11.11
Extra: Using where
1 row in set, 1 warning (0.00 sec)
(3) The query name contains 【 Hua 】 The student information of the word .
mysql> select * from student where s_name like '% Hua %';
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2022 | Zhou Huajian | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
| S2025 | Zhou Jianhua | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City |
+-------+-----------+---------------------+-------------+-----------+
2 rows in set (0.00 sec)
-- This query will not use indexes
mysql> explain select * from student where s_name like '% Hua %'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: student
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 14
filtered: 11.11
Extra: Using where
1 row in set, 1 warning (0.00 sec)
3、 ... and 、 wildcard : Underline ( _ )
When querying, it means that there is only one character in this position , The content of characters is unlimited . It has to be with LIKE Operators .
for example :
(1) The second word in the query name is 【 Hua 】 Information about our students .
mysql> select * from student where s_name like '_ Hua %';
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2022 | Zhou Huajian | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
+-------+-----------+---------------------+-------------+-----------+
1 row in set (0.00 sec)
-- This query will not use indexes
mysql> explain select * from student where s_name like '_ Hua %'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: student
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 14
filtered: 11.11
Extra: Using where
1 row in set, 1 warning (0.00 sec)
(2) Check the last name 【 Liu 】 And name a total of two words of student information .
mysql> select * from student where s_name like ' Liu _';
+-------+--------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+--------+---------------------+-------------+-----------+
| S2015 | Liu Yan | 1999-07-06 00:00:00 | 13813735225 | Xinyang City |
| S2014 | Liu Yan | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city |
+-------+--------+---------------------+-------------+-----------+
2 rows in set (0.00 sec)
-- This query uses an index idx_name
mysql> explain select * from student where s_name like ' Liu _'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: student
partitions: NULL
type: range
possible_keys: idx_name
key: idx_name
key_len: 61
ref: NULL
rows: 4
filtered: 100.00
Extra: Using index condition
1 row in set, 1 warning (0.00 sec)
(3) Inquire about phone The fourth to seventh numbers are 【2373】 Student information
-- Total at the beginning 3 Underscores
mysql> select * from student where phone like '___2373%';
+-------+--------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+--------+---------------------+-------------+-----------+
| S2014 | Liu Yan | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city |
+-------+--------+---------------------+-------------+-----------+
1 row in set (0.00 sec)
边栏推荐
- Design details related to sap e-commerce cloud Spartacus UI store
- Summary of pit websocket
- Unity metaverse (I). Ready player me & blender customize your Avatar
- Accelerating matrix vector multiplication of special matrices with FFT
- English grammar_ Demonstrative pronoun -such / the same
- Overview of data model design method
- Flutter | specifies the type of page return value
- Coloring old photos - deoldify get started quickly
- PayPal subscription process and API request
- 工作3年的测试员跳槽后工资是原来的2倍,秘诀原来是......
猜你喜欢

Image processing: Generation 3 × Window of 3

Focus on microservices

QT入门篇(2.1初入QT的开始第一个程序)

Redis | very important Middleware

Beifeng communication appeared in China (Xiamen) emergency exhibition | intelligent communication means are strong and eye-catching!

Classic example of C language - commodity inspection code

Redis cluster hash sharding algorithm (slot location algorithm)

Intelligent OCR identification of express documents helps the logistics industry to upgrade Digitalization

PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)

数仓数据标准详解-2022
随机推荐
C language: deep analysis of const keyword
这是一道大水题
Classic example of C language - commodity inspection code
Image processing: Generation 3 × Window of 3
Development of main applet for business card traffic near the map
Classic examples of C language switch case statement conversion date format
Flutter | specifies the type of page return value
Classic examples of C language - use 4 × The matrix displays all integers from 1 to 16 and calculates the sum of each row, column, and diagonal
C language book recommendation
Easy gene | target gene DNA methylation sequencing (target BS)
PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)
AWS Article 3 how to publish message to IOT mqtt in go language
The prediction of domestic AI protein structure reproduced a breakthrough and solved the 3D structure with a single sequence. Peng Jian's team: "the last piece of puzzle since alphafold2 has been comp
Expérience du système réseau: résoudre les problèmes de ping
Application of SCA on devsecops platform
AWS Part 4 one machine and one secret
Redis distributed lock to be continued
项目场景:nvidia-smi Unable to datemine the device handle for GPU 0000:01:00.0: Unknow Error
Beifeng communication appeared in China (Xiamen) emergency exhibition | intelligent communication means are strong and eye-catching!
Summary of pit websocket