7.利用正则表达式筛选 regexp
mysql> select * from pw_luck where msg regexp '1000'; +-----+-----------+------+ | uid | name | msg | +-----+-----------+------+ | 7 | zhangsan7 | 1000 | | 8 | zhangsan8 | 1000 | | 9 | zhangsan9 | 1000 | | 10 | wang5 | 1000 | +-----+-----------+------+ 4 rows in set (0.00 sec)
like 和 正则匹配 一般情况下不区分大小写 除非加入关键字 binary
mysql> select * from pw_luck where name like 'wang%'; +-----+--------+------+ | uid | name | msg | +-----+--------+------+ | 3 | wang5 | 1001 | | 10 | wang5 | 1000 | | 11 | wang5% | 2000 | | 12 | WANG5 | 1000 | +-----+--------+------+ 4 rows in set (0.00 sec) mysql> select * from pw_luck where name like binary 'wang%'; +-----+--------+------+ | uid | name | msg | +-----+--------+------+ | 3 | wang5 | 1001 | | 10 | wang5 | 1000 | | 11 | wang5% | 2000 | +-----+--------+------+ 3 rows in set (0.00 sec)
范围 []
mysql> select * from pw_luck where name regexp 'zhangsan[1-9]' -> ; +-----+-----------+------+ | uid | name | msg | +-----+-----------+------+ | 7 | zhangsan7 | 1000 | | 8 | zhangsan8 | 1000 | | 9 | zhangsan9 | 1000 | +-----+-----------+------+ 3 rows in set (0.00 sec)
mysql> select * from pw_luck where name regexp 'zhangsan[789]'; +-----+-----------+------+ | uid | name | msg | +-----+-----------+------+ | 7 | zhangsan7 | 1000 | | 8 | zhangsan8 | 1000 | | 9 | zhangsan9 | 1000 | +-----+-----------+------+ 3 rows in set (0.00 sec)
zhangsan[789]也可以写成 zhangsan[7|8|9]
转义字符: | 【】 - . 等
元字符:* 0个或多个
+ 1个或多个
? 0个或1个
{n} 指定数目
{n,}
{n,m} 范围
定位符 ^开始位置 (【】外,【】内是非的意思)
$文本结尾
mysql> select * from pw_luck where name regexp '^[l]'; +-----+------+------+ | uid | name | msg | +-----+------+------+ | 2 | lisi | 1001 | +-----+------+------+ 1 row in set (0.00 sec) mysql> select * from pw_luck where name regexp 'l'; +-----+--------+------+ | uid | name | msg | +-----+--------+------+ | 0 | Wilson | 100 | | 2 | lisi | 1001 | +-----+--------+------+ 2 rows in set (0.00 sec)