SQL zero-based introductory learning (6)
SQL zero-based introductory learning (5)
SQL wildcards
Wildcards can be used to substitute for any other character in a string.
SQL wildcards are used to search data in tables.
In SQL, the following wildcards can be used:
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data 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 | 菜鸟教程 | 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 letters "https":
SELECT * FROM Websites
WHERE url LIKE 'https%';
Execution output:
The following SQL statement selects all websites whose url contains the pattern "oo":
SELECT * FROM Websites
WHERE url LIKE '%oo%';
Execution output:
Using SQL_Wildcards
The following SQL statement selects all customers whose name starts with an arbitrary character followed by "oogle":
SELECT * FROM Websites
WHERE name LIKE '_oogle';
Execution output:
The following SQL statement selects all sites whose name starts with "G", then any character, then "o", then any character, and then "le":
SELECT * FROM Websites
WHERE name LIKE 'G_o_le';
Execution output:
Using SQL [charlist] wildcards
MySQL uses REGEXP or NOT REGEXP operators (or RLIKE and NOT RLIKE) to manipulate regular expressions.
The following SQL statement selects all sites whose name starts with "G", "F" or "s":
SELECT * FROM Websites
WHERE name REGEXP '^[GFs]';
Execution output:
The following SQL statement selects websites whose names start with letters A to H:
SELECT * FROM Websites
WHERE name REGEXP '^[A-H]';
Execution output:
The following SQL statement selects websites whose names do not start with letters A to H:
SELECT * FROM Websites
WHERE name REGEXP '^[^A-H]';
Execution output:
SQL IN operator
IN operator
The IN operator allows you to specify multiple values in the WHERE clause.
SQL IN syntax
SELECT column1, column2, ...
FROM table_name
WHERE column IN (value1, value2, ...);
Parameter description:
column1, column2, …: the name of the field to be selected, which can be multiple fields. If no field names are specified, all fields will be selected.
table_name: The name of the table to be queried.
column: The name of the field to be queried.
value1, value2, …: The value to be queried, which can be multiple values.
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data 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 | 菜鸟教程 | 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 |
+----+---------------+---------------------------+-------+---------+
IN operator instance
The following SQL statement selects all websites whose name is "Google" or "Cainiao Tutorial":
SELECT * FROM Websites
WHERE name IN ('Google','菜鸟教程');
Execution output: