MySQL like query string example

Source: Internet
Author: User
Tags mysql in mysql like query

In general, use the MySQL in query to write this

The code is as follows Copy Code

SELECT *
From ' Tb_require '
WHERE ' require_id '
In (23, 1024)

This method is generally suitable for numeric types, and if it is a string, enclose the single quotation mark. Such as:

The code is as follows Copy Code

SELECT *
From ' Tb_require '
WHERE ' Require_name '
In (' AAA ', ' bbbb ')


When querying a string, you can use like plus% if you want to blur the match. Such as:

The code is as follows Copy Code

SELECT *
From ' Tb_require '
WHERE ' require_name ' like '%aaa% '


What if there is a need to query multiple strings with ambiguity? Like Plus in, how to write?
This time you can use the MySQL concat function

The code is as follows Copy Code
SELECT * FROM Customers
WHERE ' Robert Bob Smith III, PhD. ' Like CONCAT ('% ', name, '% ')


This solves the like-in problem.

Note that the concat need to be followed by parentheses, do not have spaces, there are spaces, may be the error OH.

Note that when you use SQL mode, you cannot use = or! =; use like or not to compare operators.

SELECT field from table WHERE a field like condition

With regard to conditions, SQL provides four matching modes:

1,%: Represents any one or more characters. Can match characters of any type and length.

Like what

The code is as follows Copy Code
SELECT * FROM [user] WHERE u_name like '% III '

will be u_name as "John", "Zhang Cat Three", "three-legged Cat", "Tang Sanzang" and so on Have "three" records all find out.

Also, if you need to find a record in u_name that has "three" and "cat", use the and condition

The code is as follows Copy Code

SELECT * FROM [user] WHERE u_name like '% III ' and u_name like '% Cat '

If you use

The code is as follows Copy Code
SELECT * FROM [user] WHERE u_name like '% three% cat '

Although can search out "three-legged cat", but can not search out the conditions of the "cat three".

2,_: Represents any single character. Matches a single arbitrary character that is used to restrict the character-length statements of an expression: (you can represent a Chinese character)

Like what

The code is as follows Copy Code
SELECT * FROM [user] WHERE u_name like ' _ Three _ '

Only find "Tang Sanzang" so U_name is three words and the middle one word is "three";

Another example

The code is as follows Copy Code
SELECT * FROM [user] WHERE u_name like ' three __ ';

Only find "three-legged cat" so name is three words and the first word is "three";

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.