Home > Database > Mysql Tutorial > How to use MySQL's LIKE function for fuzzy search

How to use MySQL's LIKE function for fuzzy search

WBOY
Release: 2023-07-26 08:30:31
Original
3434 people have browsed it

How to use MySQL's LIKE function for fuzzy search

When performing database queries, sometimes we need to perform fuzzy searches based on user input to provide more accurate query results. MySQL's LIKE function is a very commonly used fuzzy search method. This article will introduce how to use MySQL's LIKE function to perform fuzzy search and provide relevant code examples.

1. Overview of LIKE function

LIKE is a function used for fuzzy search in MySQL. It can match fields in the data table based on specified patterns or wildcards.

In the LIKE function, you can use two wildcards for matching:

  1. % wildcard: represents any character and can match any number of characters.
  2. _Wildcard: represents any single character.

2. Use the LIKE function for simple fuzzy search

The following is a simple example of using the LIKE function for fuzzy search. Suppose we have a table called students that contains the name and age fields of students.

First, we can use the following SQL statement to create the students table and insert some sample data:

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    age INT
);

INSERT INTO students (name, age) VALUES
    ('Alice', 18),
    ('Bob', 20),
    ('Charlie', 22),
    ('David', 18),
    ('Eve', 22);
Copy after login
  1. Query all students with the last name 'A':
SELECT * FROM students WHERE name LIKE 'A%';
Copy after login

In the above query statement, 'A%' means the name starting with 'A', and % means that it can be followed by any character.

  1. Query all students ending with 'A':
SELECT * FROM students WHERE name LIKE '%A';
Copy after login

In the above query statement, '%A' represents the name ending with 'A', and % represents the preceding Can be any character.

  1. Query all students containing 'B':
SELECT * FROM students WHERE name LIKE '%B%';
Copy after login

In the above query statement, '%B%' means the name containing 'B', % means the preceding or It can be followed by any characters.

3. Use the LIKE function for more complex fuzzy searches

In addition to simple wildcard matching, we can also use regular expressions as the conditions of the LIKE function to match the data more accurately.

The following is an example of fuzzy search using the LIKE function and regular expressions:

  1. Query all students starting with 'A' or 'E':
SELECT * FROM students WHERE name REGEXP '^(A|E)';
Copy after login

In the above query statement, ^(A|E) represents the name starting with 'A' or 'E'.

  1. Query all students aged between 18 and 20:
SELECT * FROM students WHERE age BETWEEN 18 AND 20;
Copy after login

In the above query statement, BETWEEN means within a certain range, age BETWEEN 18 AND 20 means Aged between 18 and 20.

4. Summary

This article introduces how to use MySQL's LIKE function to perform fuzzy search. Depending on the user's needs, we can use simple wildcards or more complex regular expressions for data matching. Using the LIKE function can greatly improve the accuracy and efficiency of database queries.

I hope the content of this article will be helpful to everyone and can be applied in actual development. If you have any questions, please leave a message to discuss.

The above is the detailed content of How to use MySQL's LIKE function for fuzzy search. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template