Several Classifications of Fuzzy Query

Regarding conditions, SQL provides four matching modes:

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

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

All records with "three" in u_name "Zhang San", "Zhang Maosan", "Three-legged Cat", "Tang Sanzang", etc. will be found.

In addition, if you need to find out the records with both "three" and "cat" in u_name, please use and condition
SELECT * FROM [user] WHERE u_name LIKE '%three%' AND u_name LIKE '%cat%'

If you use SELECT * FROM [user] WHERE u_name LIKE '%three%cat%'
, although the "three-legged cat" can be searched, the matching "Zhang Maosan" cannot be searched.

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

For example, SELECT * FROM [user] WHERE u_name LIKE '_three_'
only finds "Tang Sanzang" such that the u_name is three characters and the middle one is "three";

Another example is SELECT * FROM [user] WHERE u_name LIKE 'three __';
only find "three-legged cat" such that the name is three words and the first word is "three";


3. [ ]: Indicates one of the characters listed in brackets (similar to regular expressions). Specify a character, string, or range to match against any of them.

For example, SELECT * FROM [user] WHERE u_name LIKE '[Zhang Li Wang] San'
will find "Zhang San", "Li San", "Wang San" (instead of "Zhang Li Wang San");

If there is a series of characters (01234, abcde, etc.) in [ ], it can be abbreviated as "0-4", "ae"
SELECT * FROM [user] WHERE u_name LIKE 'old [1-9]'
will find " "Old 1", "Old 2", ..., "Old 9";

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

For example, SELECT * FROM [user] WHERE u_name LIKE '[^Zhang Li Wang]Three'
will find "Zhao San", "Sun San", etc. who are not surnamed "Zhang", "Li", "Wang";

SELECT * FROM [user] WHERE u_name LIKE 'old [^1-4]';
will exclude "old 1" to "old 4", looking for "old 5", "old 6", ...

5. When the query content contains wildcard characters

Due to wildcards, the query for special characters "%", "_", and "[" cannot be implemented normally, but the special characters can be queried normally by enclosing them with "[ ]". Accordingly, we write the following function:


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


The string to be checked can be processed by this function before querying.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253308&siteId=291194637