MySQL Fuzzy query

Source: Internet
Author: User
Tags lowercase

Mysql> SELECT * from pet WHERE name like "b%";

To find the name ending with "FY":
Mysql> SELECT * from pet WHERE name like "%fy";

To find the name that contains a "w":
Mysql> SELECT * from pet WHERE name like "%w%";

To find names that contain exactly 5 characters, use the "_" mode character:
Mysql> SELECT * from pet WHERE name like "_____";

Other types of pattern matching provided by MySQL are the use of extended regular expressions. When you match this type of pattern to a test, use the

RegExp and not regexp operators (or rlike and not rlike, which are synonyms).

Some characters of the extended regular expression are:

“.” matches any single character.
A character class "[...]" Matches any character within the square brackets. For example, "[ABC]" matches "a", "B", or "C".
To name a range of characters, use a "-". "[A-z]" matches any lowercase letter, and "[0-9]" matches any
What number.
"*" matches 0 or more things in front of it. For example, "x*" matches any number of "X" characters, "[0-9]*"
Match any number of numbers, while ". *" matches any number of anything.
Regular expressions are case-sensitive, but if you want to, you can use a character class to match two ways of writing. For example
"[AA]" matches either lowercase or uppercase "a" and "[A-za-z]" to match any of the letters in both ways.
If it appears anywhere in the tested value, the pattern matches (as long as they match the entire value and the SQL pattern matches).
To locate a pattern so that it must match the start or end of the tested value, use "^" at the beginning of the pattern or the
End With "$".
To illustrate how extended regular expressions work, the like query shown below uses RegExp overrides:

To find the name that begins with "B", use "^" to match the start of the first name and "[BB]" to match lowercase or uppercase "B":
Mysql> select * FROM pet WHERE name REGEXP "^[BB]";

To find the name ending with "FY", use "$" to match the end of the name:
Mysql> select * FROM pet WHERE name REGEXP "fy$";

To find the name containing a "W", use "[WW]" to match the lowercase or uppercase "W":
Mysql> SELECT * from pet WHERE name REGEXP "[WW]";

Now that if a regular expression appears anywhere in the value and its pattern matches, you do not have to do so in the previous query in mode two
Aspect places a wildcard character so that it matches the entire value, just as if you were using an SQL schema.
To find names containing exactly 5 characters, use "^" and "$" to match the start and end of the first name, and the 5 "." Instance in
Between the two:
Mysql> select * FROM pet WHERE name REGEXP "^.....$";

You can also rewrite the previous query by using the "{n}" "Repeat N-times" operator:
Mysql> SELECT * from pet WHERE name REGEXP "^. {5}$ ";

Find numbers and other ambiguous query statements
Select * from pet where name REGEXP "[^a-za-z].";

SELECT * from oneself WHERE name like '% '
Like is the key word for fuzzy queries.
% is a wildcard, representing multiple arbitrary matches, such as '% People ' is the first half of the search is any character, the end of the People's records, similar to the% can be placed in any position in the criteria field.
_ is a single character wildcard, which can match a single character.

PHP does not need to be written like you, and the variables that appear in double quotes are automatically parsed.
$sql = "SELECT * from oneself WHERE name like '%{$colname _recordset1} ' LIMIT {$startRow _recordset1}, {$maxRows _recordset1} ";
$Recordset 1 = mysql_query ($sql) or Die (Mysql_error ());

{} on both sides of the variable is directly indicating that this is a variable and the name of the variable, if the variable effect of the array type is more obvious, it may cause an array variable name resolution error.

Full article Search

CREATE TABLE ' T3 ' (
' Name ' char ' not NULL default ',
' Name2 ' char (a) not NULL default ',
Fulltext KEY ' name ' (' name ', ' name2 ')
) Type=myisam;

#
# Dumping data for table ' T3 '
#
INSERT into ' T3 ' (' name ', ' name2 ') VALUES ("1", "Ann Change Haze");
INSERT into ' T3 ' (' name ', ' name2 ') VALUES ("2", "no Bar");
INSERT into ' T3 ' (' name ', ' name2 ') VALUES ("3", "No 1");
INSERT into ' T3 ' (' name ', ' name2 ') VALUES ("4", "No 1");
INSERT into ' T3 ' (' name ', ' name2 ') VALUES ("5", "Really not");
INSERT into ' T3 ' (' name ', ' name2 ') VALUES ("6", "Really not");

Mysql> SELECT * from T3;
+------+-------------+
¦name¦name2¦
+------+-------------+
¦1¦ ann Change Haze to ¦
¦2¦ not, ¦.
¦3¦ not, 1¦.
¦4¦ not, 1¦.
¦5¦ really not, ¦.
¦6¦ really not, ¦.
+------+-------------+
6 rows in Set (0.01 sec)
# Look up the word ' no '
Mysql> SELECT * from T3 WHERE MATCH (name,name2)
-> against (' no bar ' in BOOLEAN MODE);
+------+-------------+
¦name¦name2¦
+------+-------------+
¦2¦ not, ¦.
¦4¦ not, 1¦.
¦6¦ really not, ¦.
+------+-------------+
3 Rows in Set (0.00 sec)
# Look for any words that start with the word "no"
Mysql> SELECT * from T3 WHERE MATCH (name,name2)
-> against (' No! * ' in BOOLEAN MODE);
+------+-------------+
¦name¦name2¦
+------+-------------+
¦2¦ not, ¦.
¦4¦ not, 1¦.
¦6¦ really not, ¦.
¦3¦ not, 1¦.
+------+-------------+
4 rows in Set (0.01 sec)
# Look for any words that start with the word "no" and remove the word "really"
Mysql> SELECT * from T3 WHERE MATCH (name,name2)
-> against (' + no *-true ' in BOOLEAN MODE ');
+------+----------+
¦name¦name2¦
+------+----------+
¦2¦ not, ¦.
¦4¦ not, 1¦.
¦3¦ not, 1¦.
+------+----------+
3 Rows in Set (0.00 sec)

In the example above,
We can see that MySQL's Full-text indexing support for double-byte processing is not very good,
¦5¦ really not, ¦.
This line of records has not been queried.
This is because in the western language is separated by a space,
:(
Hopefully in a future release
MySQL can improve on this

And there's a problem with regular expressions,

# Look up the word ' no '
Mysql> select * from T3 WHERE name2 REGEXP "No";
+------+-------------+
¦name¦name2¦
+------+-------------+
¦1¦ ann Change Haze to ¦
¦2¦ not, ¦.
¦3¦ not, 1¦.
¦4¦ not, 1¦.
¦5¦ really not, ¦.
¦6¦ really not, ¦.
+------+-------------+
6 rows in Set (0.00 sec)

# Look for the word ' no '
Mysql> SELECT * from T3 WHERE name2 REGEXP "^ No";
+------+----------+
¦name¦name2¦
+------+----------+
¦2¦ not, ¦.
¦3¦ not, 1¦.
¦4¦ not, 1¦.
+------+----------+
3 Rows in Set (0.00 sec)

# Look for the word ' no ' it ends
Mysql> SELECT * from T3 WHERE name2 REGEXP "No $";
+------+-------------+
¦name¦name2¦
+------+-------------+
¦2¦ not, ¦.
¦5¦ really not, ¦.
¦6¦ really not, ¦.
+------+-------------+
3 Rows in Set (0.00 sec)

Related Article

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.