当前位置: Oracle DBA培训网-优技培训 >> MySQL培训 > MySQL培训教程 >

MySQL认证课程:标准字符串SQL模式匹配

MySQL认证课程:标准字符串SQL模式匹配

MySQL认证课程:标准字符串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 |

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

技术沙龙MORE+

Oracle技术沙龙:【数据保护顶尖技术-MAA】首次全国首播12c MAA技术 云数据库加上MAA技术组成最豪华的技...
Oracle技术沙龙第49期:【Oracle 数据库云技术分享】Oracle技术沙龙第49期:【Oracle 数据库云技术分享】本次技术沙...
Oracle技术沙龙第48期:【Oracle 12c RAC安装】小编:今天讲什么内容呢?以后有什么用呢? 大师:今天讲的是12c...
我要参加技术沙龙