MySQL fuzzy query statement collection

MySQL fuzzy query statement collection

SQL fuzzy query statement

The general fuzzy statement syntax is as follows:

SELECT field FROM table WHERE a field Like condition

Regarding conditions, SQL provides four matching modes:

1. %: represents any 0 or more characters. It can match characters of any type and length. In some cases, if it is Chinese, use two percent signs (%%) to represent it.

SELECT * FROM [user] WHERE u_name LIKE '%三%'

All records with the letter "three" in u_name, such as "Zhang San", "Zhang Mao San", "Three-legged Cat", "Tang Sanzang", etc., will be found. In addition, if you need to find records that contain both "三" and "猫" in u_name, use the and condition

SELECT * FROM [user] WHERE u_name LIKE '%三%' AND u_name LIKE '%猫%'

If using

SELECT * FROM [user] WHERE u_name LIKE '%三%猫%'

Although you can search for "三脚猫", you cannot search for "张猫三" which meets the criteria.

2. _: represents any single character. Matches a single arbitrary character, which is often used to limit the character length of the expression statement:

SELECT * FROM [user] WHERE u_name LIKE '_三_'

Only find "唐三藏" and other names whose u_name is three characters and the middle character is "三";

SELECT * FROM [user] WHERE u_name LIKE '三__';

Only find names with three characters such as "三脚猫" and the first character is "三";

3. [ ]: represents one of the characters listed in the brackets (similar to regular expressions). Specifies a character, string, or range, requiring the match to be any of them.

SELECT * FROM [user] WHERE u_name LIKE '[张李王]三'

will find "Zhang San", "Li San", "Wang San" (but not "Zhang Li Wang San");

If there is a series of characters in [ ] (such as 01234, abcde, etc.), it can be abbreviated as "0-4", "ae"

SELECT * FROM [user] WHERE u_name LIKE '老[1-9]'

will find "Old 1", "Old 2", ..., "Old 9";

4. [^ ]: represents a single character not listed in the brackets. Its value is the same as [], but it requires the matched object to be any character other than the specified characters.

SELECT * FROM [user] WHERE u_name LIKE '[^张李王]三'

We will find "Zhao San", "Sun San", etc. who are not named "Zhang", "Li", or "Wang";

SELECT * FROM [user] WHERE u_name LIKE '老[^1-4]';

We will exclude "old 1" to "old 4" and look for "old 5", "old 6", ...

5. When the query content contains wildcards

Due to the wildcard, our query statements for special characters "%", "_", and "[" cannot be implemented normally. However, we can query normally by enclosing the special characters in "[ ]". Based on this we write the following function:

function sqlencode(str)

str=replace(str,"[","[[]") 'This sentence must be at the beginning str=replace(str,"_","[_]")

str=replace(str,"%","[%]")

sqlencode=str

end function

Before querying, the string to be searched can be processed by this function.

The above is the detailed content of SQL fuzzy query statement. If you have any additions, please contact the editor of 123WORDPRESS.COM.

You may also be interested in:
  • Some common SQL statements in MySQL
  • Organize the commonly used MySql query statements (23 types)
  • The most complete MySQL query statement collection
  • MySQL statement arrangement and summary introduction
  • MySQL DML statement summary
  • MySQL statement summary
  • SQL statement arrangement used in MySQL data table

<<:  Detailed explanation of the perfect solution to the VMware black screen problem after MacOS catalina upgrade

>>:  Comprehensive understanding of Node event loop

Recommend

How to test network speed with JavaScript

Table of contents Preface Summary of the principl...

Ansible automated operation and maintenance deployment method for Linux system

Ansible is a new automated operation and maintena...

How does the composite index of MySQL take effect?

Table of contents background Understanding compos...

JavaScript to achieve magnifying glass effect

This article shares the specific code for JavaScr...

Implementation of multi-site configuration of Nginx on Mac M1

Note: nginx installed via brew Website root direc...

CSS form validation function implementation code

Rendering principle In the form element, there is...

A comprehensive summary of frequently used statements in MySQL (must read)

The knowledge points summarized below are all fre...

SQL implementation of LeetCode (177. Nth highest salary)

[LeetCode] 177.Nth Highest Salary Write a SQL que...

Introduction to several ways to introduce CSS in HTML

Table of contents 1. Embed CSS styles directly in...

One minute to experience the smoothness of html+vue+element-ui

Technology Fan html web page, you must know vue f...

How to use the EXPLAIN command in SQL

In daily work, we sometimes run slow queries to r...