mysql提供标准的sql模式匹配,以及一种基于象unix实用程序如vi、grep和sed的扩展正则表达式模式匹配的格式。 标准的sql模式匹配 sql的模式匹配允许你使用“_”匹配任何单个字符,而“%”匹配任意数目字符(包括零个字符)。在 mysql中,sql的模式缺省是忽略大
mysql提供标准的sql模式匹配,以及一种基于象unix实用程序如vi、grep和sed的扩展正则表达式模式匹配的格式。
标准的sql模式匹配
sql的模式匹配允许你使用“_”匹配任何单个字符,而“%”匹配任意数目字符(包括零个字符)。在 mysql中,sql的模式缺省是忽略大小写的。下面显示一些例子。注意在你使用sql模式时,你不能使用=或!=;而使用like或not like比较操作符。
例如,在表pet中,为了找出以“b”开头的名字:
mysql> select * from pet where name like b%;
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| buffy | harold | dog | f | 1989-05-13 | null |
| bowser | diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
为了找出以“fy”结尾的名字:
mysql> select * from pet where name like %fy;
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| fluffy | harold | cat | f | 1993-02-04 | null |
| buffy | harold | dog | f | 1989-05-13 | null |
+--------+--------+---------+------+------------+-------+
为了找出包含一个“w”的名字:
mysql> select * from pet where name like %w%;
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| claws | gwen | cat | m | 1994-03-17 | null |
| bowser | diane | dog | m | 1989-08-31 | 1995-07-29 |
| whistler | gwen | bird | null | 1997-12-09 | null |
+----------+-------+---------+------+------------+------------+
为了找出包含正好5个字符的名字,使用“_”模式字符:
mysql> select * from pet where name like _____;
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| claws | gwen | cat | m | 1994-03-17 | null |
| buffy | harold | dog | f | 1989-05-13 | null |
+-------+--------+---------+------+------------+-------+