Details about the like operator in MySQL

Details about the like operator in MySQL

1. Introduction

When 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:

  • % Wildcard, used to match multiple characters
  • _ character, used to match a single character

Wildcards can be matched in six different ways depending on where they are located:

Matching method effect
%xx Indicates right match. The xx characters on the right must be exactly the same. The left side can be any character or no character.
_xx Indicates right match. The xx characters on the right must be exactly the same. The left side can be any character, but it must be a character.
xx% Indicates left match. The xx characters on the right need to be completely equal. The right side can be any character or no character.
xx_ Indicates left match. The xx characters on the left must be completely equal. The right side can be any character, but it must be a character.
%xx% Indicates a middle match. The middle must be completely equal. The left and right sides can be any characters. There can be no other characters on the left and right sides.
xx Indicates a middle match. The middle must be completely equal. The left and right sides can be any character. The left and right sides must be a character.

2. Main text

First, 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 %xx , xx% , and %xx% . The following will demonstrate the simple usage of the three.

need:

Query the user whose last name is Zhang in the user table

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 user table whose names end with seven

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 user table for users whose names contain the character "李"

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 _ wildcard and % wildcard is that _ matches only one character and must match one character; while % can match multiple characters, even 0 characters.

need:

Query the user table for users whose last name is Li and whose first name has only two Chinese characters

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 user user named three in the user table

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 user table for users whose names are three "zi" and the second "zi" is "ma"

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 precautions

Wildcards 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.

  • The principle of not using it when possible can avoid all the problems caused by wildcards, so if other operators can be used to search, do not use LIKE.
  • Where wildcards are used, try to narrow the search scope. If there are multiple search conditions, consider whether the wildcard can be placed after other filter conditions.
  • Pay special attention to the selection and position of wildcards. You can refer to the six matching methods to choose the one that suits you best.

This is the end of this article about the details of MySQL like operator. For more information about the MySQL like operator, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the mysql database LIKE operator in python

<<:  React Router V6 Updates

>>:  A brief discussion on HTML special character encoding CSS3 content: "I am a special symbol"

Recommend

win10 docker-toolsbox tutorial on building a php development environment

Download image docker pull mysql:5.7 docker pull ...

MySQL SQL statement analysis and query optimization detailed explanation

How to obtain SQL statements with performance iss...

Vue mobile terminal realizes finger sliding effect

This article example shares the specific code for...

How to remotely connect to the cloud server database using Navicat

It is very convenient to connect to a remote serv...

VMware configuration hadoop to achieve pseudo-distributed graphic tutorial

1. Experimental Environment serial number project...

3D tunnel effect implemented by CSS3

The effect achievedImplementation Code html <d...

In-depth understanding of umask in new linux file permission settings

Preface The origin is a question 1: If your umask...

MySQL 8.0.20 installation tutorial and detailed tutorial on installation issues

Original address: https://blog.csdn.net/m0_465798...

JS implementation of carousel example

This article shares the specific code of JS to im...