>데이터 베이스 >MySQL 튜토리얼 >mysql学习--mysql必知必会1_MySQL

mysql学习--mysql必知必会1_MySQL

WBOY
WBOY원래의
2016-06-01 13:13:521138검색

如下为mysql必知必会第九章开始:

正则表达式用于匹配特殊的字符集合。mysql通过where子句对正则表达式提供初步的支持。

关键字regexp用来表示后面跟的东西作为正则表达式处理。

(.)是正则表达式的一个符号,表示匹配任意一个字符:

mysql> select prod_name	-> from products	-> where prod_name regexp '.000'	-> order by prod_name;+--------------+| prod_name	|+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.14 sec)

|匹配符:

表示匹配其中之一

mysql> select prod_name	-> from products	-> where prod_name REGEXP '1000|2000'	-> ORDER BY prod_name;+--------------+| prod_name	|+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.00 sec)


[]匹配符: 匹配几个字符之一

2 rows in set (0.00 sec)mysql> select prod_name	-> from products	-> where prod_name regexp '[123] Ton'	-> ;+-------------+| prod_name |+-------------+| 1 ton anvil || 2 ton anvil |+-------------+2 rows in set (0.00 sec)
mysql> select prod_name from products where prod_name regexp '[1-5] Ton';+--------------+| prod_name|+--------------+| .5 ton anvil || 1 ton anvil|| 2 ton anvil|+--------------+3 rows in set (0.02 sec)
mysql> select prod_name from products where prod_name regexp '[^1-3] Ton';+--------------+| prod_name|+--------------+| .5 ton anvil |+--------------+1 row in set (0.00 sec)


匹配特殊字符,必须用//为前导。

mysql> select prod_name from products where prod_name regexp '//.' ;+--------------+| prod_name|+--------------+| .5 ton anvil |+--------------+1 row in set (0.00 sec)


匹配字符类:

mysql> select prod_name from products where prod_name REGEXP '//([0-9] sticks?//)' order by prod_name;+----------------+| prod_name|+----------------+| TNT (1 stick)|| TNT (5 sticks) |+----------------+2 rows in set (0.05 sec)mysql> select prod_name from products where prod_name REGEXP '[[:digit:]]{4}' order by prod_name;+--------------+| prod_name|+--------------+| JetPack 1000 || JetPack 2000 |+--------------+2 rows in set (0.00 sec)

定位符用法:

mysql> select prod_name	-> from products	-> where prod_name REGEXP '^[0-9//.]'	-> order by prod_name;+--------------+| prod_name	|+--------------+| .5 ton anvil || 1 ton anvil|| 2 ton anvil|+--------------+3 rows in set (0.00 sec)


以上都是mysql正则表达式的用法。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.