Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL Wildcards

SQL Wildcards


Wildcards can be used to substitute for any other character in a string.


SQL Wildcards

In SQL, wildcards are used together with the SQL LIKE operator.

SQL wildcards are used to search for data in a table.

In SQL, the following wildcards are available:

Wildcard Description
% Substitutes for zero or more characters
_ Substitutes for a single character
[charlist] Any single character within a character column
[^charlist]
 或       

 [!*charlist*] | 不在字符列中的任何单一字符 |


Demonstration Database

In this tutorial, we will use the RUNOOB sample database.

Below is the data selected from the "Websites" table:



mysql> SELECT * FROM Websites;

+----+---------------+---------------------------+-------+---------+

| id | name          | url                       | alexa | country |

+----+---------------+---------------------------+-------+---------+

|  1 | Google        | https://www.google.cm/    |     1 | USA     |

|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |

|  3 | Runoob Tutorial       | http://www.runoob.com/    |  5000 | USA     |

|  4 | 微博           | http://weibo.com/         |    20 | CN      |

|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |

|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |

+----+---------------+---------------------------+-------+---------+


Using the SQL % Wildcard

The following SQL statement selects all websites whose url starts with the letter "https":

Example

SELECT * FROM Websites

WHERE url LIKE 'https%';

Execution output:

The following SQL statement selects all websites whose url contains the pattern "oo":

Example

SELECT * FROM Websites

WHERE url LIKE '%oo%';

Execution output:


Using the SQL _ Wildcard

The following SQL statement selects all customers whose name starts with any single character followed by "oogle":

Example

SELECT * FROM Websites

WHERE name LIKE '_oogle';

Execution output:

The following SQL statement selects all websites whose name starts with "G", followed by any single character, then "o", then any single character, and then "le":

Example

SELECT * FROM Websites

WHERE name LIKE 'G_o_le';

Execution output:


Using the SQL [charlist] Wildcard

In SQL, the [charlist] wildcard specifies a list of characters and matches any one character in that list.

It is commonly used with the LIKE operator for pattern matching, for example in a WHERE clause.

The following SQL statement selects all websites whose name starts with "G", "F", or "s":

Example

SELECT * FROM Websites

WHERE name REGEXP '^[GFs]';

Execution output:

The following SQL statement selects websites whose name starts with a letter from A to H:

Example

SELECT * FROM Websites

WHERE name REGEXP '^[A-H]';

Execution output:

The following SQL statement selects websites whose name does not start with a letter from A to H:

Example

SELECT * FROM Websites

WHERE name REGEXP '^[^A-H]';

Execution output: