How to use SQL statements for fuzzy search?

In the previous article, we introduced how to use the query conditions in the WHERE clause to filter data, including comparison operators, logical operators, and null value judgments. At the same time, we also mentioned that the LIKE operator can be used for fuzzy search of strings. In this article, we will discuss fuzzy matching in SQL.

When the information you need to find is not sure, for example, you only remember part of an employee's name, you can use the fuzzy search function to search. SQL provides two fuzzy matching methods: LIKE operator and regular expression function .

LIKE operator

The following sentence finds employees whose names begin with "Zhao":

SELECT emp_id, emp_name, sex
  FROM employee
 WHERE emp_name LIKE '赵%';

This statement uses a new operator: LIKE. LIKE is used to specify a pattern and return data matching the pattern . The result of this statement is as follows:

avatar

The LIKE operator supports two wildcards for specifying patterns:

  • % , The percent sign can match zero or more arbitrary characters.
  • _ , The underscore can match any character.

Here are some patterns and matching strings:

  • LIKE 'en%', Match the string starting with "en", such as "english languages", "end";
  • LIKE '%en%', Matches the string containing "en", such as "length", "w

Guess you like

Origin blog.csdn.net/horses/article/details/108729114