Home >Database >SQL >SQL fuzzy query statement

SQL fuzzy query statement

angryTom
angryTomOriginal
2020-02-06 10:50:5231777browse

SQL fuzzy query statement

SQL fuzzy query statement

The general fuzzy statement syntax is as follows:

SELECT 字段 FROM 表 WHERE 某字段 Like 条件

About the conditions, SQL provides There are four matching modes:

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

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

will find all the records with "three" in u_name as "Zhang San", "Zhang Cat San", "Three-legged Cat", "Tang Sanzang", etc. In addition, if you need to find records with both "三" and "cat" in u_name, please use the and condition

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

If you use

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

, you can search for "三级" Cat", but the search for "Zhang Cat San" that meets the conditions cannot be found.

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

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

Only find "Tang Sanzang" so that u_name is three characters and the middle character is "三" ;

SELECT * FROM [user] WHERE u_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). Specify a character, string, or range, requiring the matched object to be any one of them.

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

will find "Zhang San", "Li San", "Wang San" (instead of "Zhang Li Wang San");

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

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

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

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

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

Will find "Zhao San", "Sun San", etc. whose surnames are not "Zhang", "Li", "Wang";

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

Will exclude "Old 1" to "Old 4" and search for "Old 5", "Old 6",...

5. When the query content contains wildcards

Due to wildcards, our query statements for special characters "%", "_", and "[" cannot be implemented normally. However, the special characters can be queried normally if they are enclosed in "[ ]". Based on this, we write the following function:

function sqlencode(str)
str=replace(str,"[","[[]") '此句一定要在最前
str=replace(str,"_","[_]")
str=replace(str,"%","[%]")
sqlencode=str
end function

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

PHP Chinese website has a large number of free SQL tutorials, everyone is welcome to learn!

The above is the detailed content of SQL fuzzy query statement. For more information, please follow other related articles on the PHP Chinese website!

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