The usage of like in sql statement

In the SQL Structured Query Language, the LIKE statement plays a vital role.
The syntax format of the LIKE statement is: select from table name where field name like corresponding value (substring), it is mainly for character fields, and its function is to retrieve the corresponding substring in a character field column.
Suppose there is a table table1 in a database, and there are two fields in table1, namely name and sex, both of which are character data. Now we want to query the records starting with "Zhang" in the name field, the statement is as follows:
select
from table1 where name like "ZHANG "
If we want to query the records ending with "ZHANG", the statement is as follows:
select
from table1 where name like " Zhang"
uses the wildcard "
" here. It can be said that the like statement is inseparable from the wildcard. Let's take a closer look at wildcards.
Matching type  
Pattern
example and representative value
description
Multiple characters
c
c represents cc, cBc, cbc, cabdfec, etc.
It is the same as the wildcard in DOS commands, representing multiple characters.

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

Special characters [ ]
a[
]a represent a
instead of a

The single character ?
b?b stands for brb, and bFb is
equivalent to ? in the DOS command. Wildcard, representing a single character

The single number #
k#k represents k1k, k8k, and k0k, which
are roughly the same as above, except that the generation can only represent a single number.

character range

  • [az] represents any of the 26 letters from a to z to specify any one of a range
    Continue to
    exclude [! character] [!az] represents 9, 0, %, * etc. It only represents a single character
    Number excludes [! number ] [!0-9] represents A, b, C, d is equivalent to the
    combination type character [range type] character cc[!ad]# represents ccF#, etc. It can be used in combination with several other ways.
    Suppose there are the following records in table1 :
        name sex
    Zhang Xiaoming male Li Mingming male
        Li Atian
        Female
        Wang 5 five male
        Wang Qing five male

Let's illustrate with an example:
Example 1, query the name field containing the word "ming".
      select from table1 where name like '%ming%'
Example 2, the query name field starts with the word "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 '%[az]%'
Example 5, the query name field does not contain numbers.
      select
from table1 where name like '%[!0-9]%'

It is obvious what values ​​can be listed in the above example. But here, what we focus on is the difference between the wildcard " " and "%".
Many friends will ask, why do I use "%" instead of "
" when I have some individual representations of all characters in the above query?
Let's take a look at the results of the following examples:
  select from table1 where name like clear
select
from table1 where name like % clear % You
will see that the previous statement lists all records, and the latter record lists It is a record containing "clear" in the name field, so it is better to use "%" instead of " " when we make a query that the character field contains a substring. When using " ", it is only at the beginning or only at the end. , but cannot replace any character with "*" at both ends.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848358&siteId=291194637