Like statement in SQL Structured Query Language

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. Currently, 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] indicates 9, 0, %, *, and so on. It only means that a single character number is excluded [! Number] [! 0-9] indicates a, B, c, d, and so on. The preceding combination type character [range type] character CC [! A-d] # indicates CCF # 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 Li *
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 of my friends may ask, why do I use "%" instead of "*" to individually represent all characters in the preceding queries? 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 %
We can see that the previous statement lists all records, and the next record lists the records whose name field contains "Ming". Therefore, "%" is the best choice for a query that contains a substring in a struct field instead of "*". When "*" is used, it is only at the beginning or only at the end, but cannot replace any character with "*" at both ends.

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.