Home  >  Article  >  Backend Development  >  Explanation of SQL wildcard related knowledge

Explanation of SQL wildcard related knowledge

jacklove
jackloveOriginal
2018-05-08 09:58:291985browse

When searching for data in the database, you can use SQL wildcards. This article will explain the relevant knowledge in detail.

You can use SQL wildcards when searching for data in a database.

SQL Wildcards

SQL wildcards can replace one or more characters when searching for data in a database.

SQL wildcards must be used with the LIKE operator.

In SQL, you can use the following wildcards:

Use the % wildcard character

Example 1

Now, we want to extract data from the "Persons" table above Select people living in cities starting with "Ne":

We can use the following SELECT statement:

SELECT * FROM Persons
WHERE City LIKE 'Ne%'

Example 2

Next, we want to start with " Persons" table to select people living in cities containing "lond":

We can use the following SELECT statement:

SELECT * FROM Persons
WHERE City LIKE '%lond%'

Use _ wildcard

Example 1

Now, we want to select people whose first character is "eorge" after the first character of their name from the above "Persons" table:

We can use the following SELECT statement:

SELECT * FROM Persons
WHERE FirstName LIKE '_eorge'

Example 2

Next, we want the last name of this record selected from the "Persons" table to start with "C", then any character, then "r", then any character, then "er":

We can use the following SELECT statement:

SELECT * FROM Persons
WHERE LastName LIKE 'C_r_er'

Use [charlist] wildcard

Example 1

Now, we want to select people whose cities start with "A" or "L" or "N" from the "Persons" table above:

We can use the following SELECT statement :

SELECT * FROM Persons
WHERE City LIKE '[ALN]%'

Example 2

Now, we want to select people whose cities do not start with "A" or "L" or "N" from the "Persons" table above:

We can use the following SELECT statement:

SELECT * FROM Persons
WHERE City LIKE '[!ALN]%'

This article provides a relevant explanation of wildcards. For more learning materials, please pay attention to the php Chinese website.

Related recommendations:

Explanation about the SQL LIKE operator

Explanation about the SQL TOP clause

About the analysis of SQL SELECT DISTINCT statement

The above is the detailed content of Explanation of SQL wildcard related knowledge. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn