1. IntroductionWhen filtering unknown or partially known values, you can use the like operator; the like operator is used for fuzzy matching. Like supports two wildcards, they are:
Wildcards can be matched in six different ways depending on where they are located:
2. Main textFirst, prepare a User table. The DDL and table data are as follows and can be copied and used directly. SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'User name', `age` int(11) NOT NULL COMMENT 'Age', `sex` smallint(6) NOT NULL COMMENT 'Gender', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, '李子8', 18, 1); INSERT INTO `user` VALUES (2, '张三', 22, 1); INSERT INTO `user` VALUES (3, '李四', 38, 1); INSERT INTO `user` VALUES (4, '王五', 25, 1); INSERT INTO `user` VALUES (5, '六麻子', 13, 0); INSERT INTO `user` VALUES (6, '田七', 37, 1); INSERT INTO `user` VALUES (7, 'Thank you', 18, 1); SET FOREIGN_KEY_CHECKS = 1; The initial order of the copied code data is as follows: mysql> select * from user; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | Plum 8 | 18 | 1 | | 2 | Zhang San | 22 | 1 | | 3 | Li Si | 38 | 1 | | 4 | Wang Wu | 25 | 1 | | 5 | Liu Mazi | 13 | 0 | | 6 | Tianqi | 37 | 1 | | 7 | Thank you | 18 | 1 | +----+--------+-----+-----+ 7 rows in set (0.00 sec) 2.1 % Wildcard There are three matching methods for the % wildcard, namely need: Query the user whose last name is Zhang in the Statement: mysql> select * from user where name like '张%'; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 2 | Zhang San | 22 | 1 | +----+------+-----+-----+ 1 row in set (0.00 sec) need: Query the users in the Statement: mysql> select * from user where name like '%七'; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 6 | Tianqi | 37 | 1 | +----+------+-----+-----+ 1 row in set (0.00 sec) need: Query the Statement: mysql> select * from user where name like '%李%'; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | Plum 8 | 18 | 1 | | 3 | Li Si | 38 | 1 | +----+--------+-----+-----+ 2 rows in set (0.00 sec) 2.2 _Wildcard The difference between the need: Query the Statement: mysql> select * from user where name like '李_'; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 3 | Li Si | 38 | 1 | +----+------+-----+-----+ 1 row in set (0.00 sec) need: Query Statement: mysql> select * from user where name like '_三'; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 2 | Zhang San | 22 | 1 | +----+------+-----+-----+ 1 row in set (0.00 sec) need: Query the Statement: mysql> select * from user where name like '_麻_'; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 5 | Liu Mazi | 13 | 0 | +----+--------+-----+-----+ 1 row in set (0.00 sec) 2.3 Wildcard usage precautionsWildcards are very powerful, and I believe many people use them frequently, but string matching is often not a particularly fast thing. Therefore, there are some things we need to keep in mind when using wildcards.
This is the end of this article about the details of You may also be interested in:
|
>>: A brief discussion on HTML special character encoding CSS3 content: "I am a special symbol"
A few days ago, I discovered that my website was ...
Download image docker pull mysql:5.7 docker pull ...
How to obtain SQL statements with performance iss...
This article introduces an example of using HTML+...
Problem Reproduction Alibaba Cloud Server, using ...
Preface As you all know, we have encountered many...
This article example shares the specific code for...
It is very convenient to connect to a remote serv...
1. Experimental Environment serial number project...
After switching from Vue2's writing style to ...
The effect achievedImplementation Code html <d...
Preface The origin is a question 1: If your umask...
Original address: https://blog.csdn.net/m0_465798...
This article shares the specific code of JS to im...
Table of contents 1- Error details 2-Single Solut...