The implementation method of PHP fuzzy query (recommended) _php instance

Source: Internet
Author: User

Mode query

1. SQL Match mode

2. Regular expression matching pattern (generally not recommended)

SQL match mode

1. Use SQL match mode, cannot use operator = or! =, instead of using the operator like or not like;

2. Using SQL matching mode, MySQL provides 2 wildcard characters.

% represents any number of any characters (including 0)

_ Represents any single character

3. Using SQL matching mode, if the matching format does not contain any of the above 2 wildcard characters, the query's effect equals = or! =

4. Use SQL match mode, match case case-insensitive

#查询用户名以某个字符开头的用户
#查询用户名以字符 ' l ' User: l%
SELECT * from user WHERE username like ' l% ';
#查询用户名以某个字符结尾的用户
#查询用户名以字符 ' e ' end User: e%
SELECT * from user WHERE username like ' e% ';
#查询用户名包含某个字符的用户
#查询用户名包含字符 ' o ':%o%
SELECT * from user WHERE username like '%o% ';
#查询包含三个字符的用户
SELECT * from user WHERE username like ' ___ ';
#查询用户名第二个字符为o的用户: _o%
SELECT * from user WHERE username like ' _o% ';

Regular expression matching pattern

wildcard character (regular expression)

. Match any single character

* Match 0 or more characters in front of it

x* means match any number of x characters

[..] Match any character in the brackets
[ABC] match character AB or C
[A-z] matches any letter
[0-9] matches any number
[0-9]* matches any number of any numbers
[a-z]* matches any number of letters

^ representation begins with a character or string

^a, beginning with the letter A

$ indicates ending with a character or string

s$ the end of the letter S

The operators used in a regular expression matching pattern are:

REGEXP or not REGEXP (rlike or not rlike)

Note: The regular expression matches the pattern, and its regular expression appears anywhere in the matching field.

Even if the pattern matches, it is not necessary to put a wildcard on both sides to make it match;

If only wildcard characters are used. To match, assuming N, then its matching pattern is expressed to be greater than or equal to N;

How do you understand the above remark?

So

... Match data greater than or equal to 3 characters
.... Match data greater than or equal to 4 characters
User with #查询用户名以字符 L: ^l;
#正则表达式写法

SELECT * FROM user WHERE username REGEXP ' ^l ';
#sql匹配模式写法:
SELECT * from user WHERE username like ' l% ';
#查询用户名正好是三个字符的用户: ^...$;
#sql匹配模式写法:
SELECT * from user WHERE username like ' ___ ';
#正则表达式写法
SELECT * from user WHERE username REGEXP ' ^...$ ';

The above PHP Fuzzy query implementation method (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.