Home Backend Development PHP Tutorial mysql regular matching fuzzy query for a certain field

mysql regular matching fuzzy query for a certain field

Jan 27, 2020 pm 09:18 PM
regular

mysql regular matching fuzzy query for a certain field

When we query a certain field, sometimes we only want to match a certain piece of data. For example, if we want to query all the keywords of this article to see if a certain keyword exists, we often need to Matching, let's explain how to match and query a certain keyword value

The syntax of SQL fuzzy query is

"SELECT column FROM table WHERE column LIKE ';pattern';".

SQL provides four matching modes:

1. % represents any 0 or more characters. The following statement:

SELECT * FROM user WHERE name LIKE ';%三%';

will change the name to "Zhang San", "Three-legged Cat", "Tang Sanzang", etc. Find all characters with "three";

2. _ represents any single character. Statement:

SELECT * FROM user WHERE name LIKE ';_三_';

Only find "Tang Sanzang" whose name has three characters and the middle character is "三" ;

SELECT * FROM user WHERE name LIKE ';三__';

Only find "three-legged cat" whose name has three characters and the first character is "三" ;

3. [ ] represents one of the characters listed in brackets (similar to a regular expression). Statement:

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

will find "Zhang San", "Li San", "Wang San" (and Not "Zhang Li Wang San");

If [ ] contains a series of characters (01234, abcde, etc.), it can be abbreviated as "0-4", "a-e"

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

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

If you want to find the "-" character, please put it first: ';Zhang San[-1-9]';

4. [^ ] means a single character not listed in brackets. Statement:

SELECT * FROM user WHERE name LIKE ';[^Zhang Liwang]三';

will find "" whose surname is not "Zhang", "Li", or "Wang" Zhao San", "Sun San", etc.;

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

will exclude "老1" to "老4 "Look for "Old 5", "Old 6",..., "Old 9".

! The last thing is the point!

Due to wildcards, our query statements for special characters "%", "_", "[", and "';" cannot be implemented normally, and it is convenient to enclose special characters with "[ ]" Can be queried normally. Based on this, we write the following function:

function sqlencode(str)

str=replace(str,"';","';';")

str =replace(str,"[","[[]") ';This sentence must come first

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

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

sqlencode=str

end function

The above is the detailed content of mysql regular matching fuzzy query for a certain field. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to replace a string starting with something with php regular expression How to replace a string starting with something with php regular expression Mar 24, 2023 pm 02:57 PM

PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

How to match multiple words or strings using Golang regular expression? How to match multiple words or strings using Golang regular expression? May 31, 2024 am 10:32 AM

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

How to use regular expressions to remove Chinese characters in php How to use regular expressions to remove Chinese characters in php Mar 03, 2023 am 10:12 AM

How to remove Chinese in PHP using regular expressions: 1. Create a PHP sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff]*)/i', '',$a);" The regular method can remove Chinese characters from the query results.

How to use regular matching to remove html tags in php How to use regular matching to remove html tags in php Mar 21, 2023 pm 05:17 PM

In this article, we will learn how to remove HTML tags and extract plain text content from HTML strings using PHP regular expressions. To demonstrate how to remove HTML tags, let's first define a string containing HTML tags.

Sharing tips on using PHP regular expressions to implement Chinese replacement function Sharing tips on using PHP regular expressions to implement Chinese replacement function Mar 24, 2024 pm 05:57 PM

Sharing tips on using PHP regular expressions to implement the Chinese replacement function. In web development, we often encounter situations where Chinese content needs to be replaced. As a popular server-side scripting language, PHP provides powerful regular expression functions, which can easily realize Chinese replacement. This article will share some techniques for using regular expressions to implement Chinese substitution in PHP, and provide specific code examples. 1. Use the preg_replace function to implement Chinese replacement. The preg_replace function in PHP can be used

How to verify if a URL is HTTPS protocol using PHP regex How to verify if a URL is HTTPS protocol using PHP regex Jun 24, 2023 am 08:16 AM

Website security has attracted more and more attention, and using the HTTPS protocol to ensure the security of data transmission has become an important part of current website development. In PHP development, how to use regular expressions to verify whether the URL is HTTPS protocol? here we come to find out. Regular expression Regular expression is an expression used to describe rules. It is a powerful tool for processing text and is widely used in text matching, search and replacement. In PHP development, we can use regular expressions to match http in the URL

An in-depth explanation of PHP regular expression escaping An in-depth explanation of PHP regular expression escaping Mar 21, 2023 pm 02:52 PM

​Regular expressions are a powerful tool for matching strings and can facilitate string manipulation. However, in the process of writing regular expressions, sometimes you may need to match some special characters, such as "\", "|", "{", etc. These characters have special meanings in regular expressions and need to be escaped.

PHP uses regular expressions to verify whether the input data is a number PHP uses regular expressions to verify whether the input data is a number Mar 21, 2023 pm 02:07 PM

When developing websites and applications, it is crucial to verify the correctness and format of input data. In PHP, regular expressions are a powerful tool that can be used to validate and format various types of data. Among them, validating input data as numbers is a very common requirement. In this article, we will discuss how to use regular expressions in PHP to validate input data as numbers.

See all articles