在 SQL 中,通配符与 SQL LIKE 操作符一起使用。
SQL 通配符用于搜索表中的数据。
在 SQL 中,可使用一下通配符:
通配符 | 描述 |
---|---|
% | 替代 0 个或多个字符 |
_ | 替代一个字符 |
[charlist] | 字符列中的任何单一字符 |
[^charlist] 或 [!charlist] |
不在字符列中的任何单一字符 |
在本教程中,我们将使用 Web3 样本数据库。
下面是选自 "Websites" 表的数据:
+----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 3 | 芝麻教程 | http://www.web3.xin/ | 4689 | CN | | 4 | 微博 | http://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | | 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND | +----+---------------+---------------------------+-------+---------+
下面的 SQL 语句选取 url 以字母 "https" 开始的所有网站:
SELECT * FROM Websites WHERE url LIKE 'https%';执行输出结果:
mysql> select * from websites where url like 'https%'; +----+----------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+----------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | +----+----------+---------------------------+-------+---------+ 3 rows in set (0.00 sec)下面的 SQL 语句选取 url 包含模式 "oo" 的所有网站:
SELECT * FROM Websites WHERE url LIKE '%oo%';执行输出结果:
mysql> SELECT * FROM Websites WHERE url LIKE '%oo%'; +----+----------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+----------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | +----+----------+---------------------------+-------+---------+ 2 rows in set (0.00 sec)
下面的 SQL 语句选取 name 以一个任意字符开始,然后是 "oogle" 的所有客户:
SELECT * FROM Websites WHERE name LIKE '_oogle';执行输出结果:
mysql> select * from websites where name like '_oogle'; +----+--------+------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------+------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | +----+--------+------------------------+-------+---------+ 1 row in set (0.00 sec)下面的 SQL 语句选取 name 以 "G" 开始,然后是一个任意字符,然后是 "o",然后是一个任意字符,然后是 "le" 的所有网站:
SELECT * FROM Websites WHERE name LIKE 'G_o_le';执行输出结果:
mysql> SELECT * FROM Websites WHERE name LIKE 'G_o_le'; +----+--------+------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------+------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | +----+--------+------------------------+-------+---------+ 1 row in set (0.00 sec)
MySQL 中使用 REGEXP 或 NOT REGEXP 运算符 (或 RLIKE 和 NOT RLIKE) 来操作正则表达式。
下面的 SQL 语句选取 name 以 "G"、"F" 或 "s" 开始的所有网站:
SELECT * FROM Websites WHERE name REGEXP '^[GFs]';执行输出结果:
mysql> select * from websites where name regexp '^[GFs]'; +----+---------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+---------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | | 9 | stackoverflow | http://stackoverflow.com/ | NULL | IND | +----+---------------+---------------------------+-------+---------+ 3 rows in set (0.00 sec)下面的 SQL 语句选取 name 以 A 到 H 字母开头的网站:
SELECT * FROM Websites WHERE name REGEXP '^[A-H]';执行输出结果:
mysql> select * from websites where name regexp '^[A-H]'; +----+----------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+----------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | +----+----------+---------------------------+-------+---------+ 2 rows in set (0.00 sec)下面的 SQL 语句选取 name 不以 A 到 H 字母开头的网站:
SELECT * FROM Websites WHERE name REGEXP '^[^A-H]';执行输出结果:
mysql> select * from websites where name regexp '^[^A-H]'; +----+---------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+---------------+---------------------------+-------+---------+ | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 3 | 芝麻教程 | http://www.web3.xin/ | 5000 | USA | | 4 | 微博 | http://weibo.com/ | 20 | CN | | 9 | stackoverflow | http://stackoverflow.com/ | NULL | IND | +----+---------------+---------------------------+-------+---------+ 4 rows in set (0.00 sec)