Detailed analysis of like usage in SQL statements

Source: Internet
Author: User

In SQL Structured Query Language, LIKE statements play a vital role.
The syntax format of the LIKE statement is: select * from table name where field name like corresponding value (substring), which is mainly for struct fields, it is used to retrieve corresponding substrings in a struct field column.

Assume that a database has a table named table1, and table1 has two fields, namely name and sex, which are full of metadata data. Now we want to query records starting with "Zhang" in the name field. The statement is as follows:
Select * from table1 where name like "sheet *"
To query records ending with "sheets", the statement is as follows:
Select * from table1 where name like "* Sheets"
The wildcard "*" is used here. It can be said that the like statement is inseparable from the wildcard. The following describes the wildcard characters in detail.

Matching type
Mode
Examples and representative values
Description

Multiple characters
*
C * c stands for cc, cBc, cbc, cabdfec, etc.
It is equivalent to a wildcard in the doscommand and represents multiple characters.

Multiple characters
%
% C % Represents agdcagd, etc.
This method is used in many programs, mainly for queries containing substrings.

Special characters
[*]
A [*] a represents a *
Replace *

Single Character
?
B? B Represents brb, bFb, etc.
Is the same as that in the doscommand? Wildcard, representing a single character

Single digit
#
K # k Represents k1k, k8k, k0k
The same is roughly the same as that of a single number.

Character range
-[A-z] indicates any of the 26 letters from a to z specifying a range.
Continue
Exclude [! Character] [! A-z] represents 9, 0, %, *, and so on, it only represents a single character
Number exclusion [! Number] [! 0-9] indicates A, B, C, d, and so on.
Combination type character [range type] character cc [! A-d] # indicates ccF # and can be combined with other methods

Assume that Table 1 contains the following records:
Name sex
Zhang Xiaoming male
Li tomorrow male
Li a tiannvwa
Wang 5, five male
Wang qingwu male

The following is an example:
Example 1: the query name field contains the word "Ming.
Select * from table1 where name like '% Ming %'
Example 2: The name field starts with "Li.
Select * from table1 where name like 'Lee *'
Example 3: the query name field contains numbers.
Select * from table1 where name like '% [0-9] %'
Example 4: the query name field contains lowercase letters.
Select * from table1 where name like '% [a-z] %'
Example 5: the query name field does not contain numbers.
Select * from table1 where name like '% [! 0-9] %'

The values listed in the preceding example are obvious. But here, it is important to note the difference between the wildcard "*" and "%.
Many may ask, why do I use "%" instead of "*" When individually representing all characters in the preceding query?
First, let's take a look at the results of the following example:
Select * from table1 where name like * Ming *
Select * from table1 where name like % Ming %

As you can see, the previous statement lists all records, and the next record lists the records whose name field contains "Ming,
Therefore, we recommend that you use "%" instead of "*" when using a struct field to contain a substring. when "*" is used, it can only start or end with "*", but cannot replace any character with "*" at both ends.

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.