正则表达式
“^”代表查询以特定字符或者字符串开头的记录
Select * from cpmpany where name regexp ‘^李’;
“$”代表查询以特定字符或者字符串结尾的记录
Select * from cpmpany where name regexp ‘三$’;
“.” 匹配字符串中的任意一个字符
Select * from cpmpany where name regexp ‘王.’;
为了方便后面的函数运行,在表company中插入新的行phone_number
ALTER TABLE cpmpany ADD phone_number varchar(12);
在phone_number列插入数据
UPDATE company SET pnone_number = '18888889955' WHERE staff_id = 1;
按此语句依次插满该列10行数据
“_” 匹配字符串中的任意一个字符
注意“.”只能匹配regexp,不能用在like上。
Select * from company where name regexp ‘王.’;
“[]” [..]字符集合。匹配所包含的任意一个字符。
Select * from company where phone_number regexp ‘[2-4]’; #包含2-4范围内的数据全部显示
“[^字符集合]”匹配指定字符以外的字符
Select * from company where phone_number regexp '[^0-9]'; #显然没有数据
Select * from company where phone_number regexp '[^1-9]'; #输出包含0的一行。
“|”。只要匹配这些字符串中的任意一个即可(和OR的作用差不多)
“*”和“+”
匹配多个字符,*和+都可以匹配多个该符号之前的字符。
“+”:至少表示一个字符, “*”:可以表示0个字符
Select * from company where phpne_number regexp ‘0*9’;
Select * from company where phpne_number regexp ‘8+9’;
‘{m}’或者’{m,n}’来指定字符串连续出现的次数
{m}:m是一个非负整数。匹配确定的 m 次。
{m,n}:m 和 n 均为非负整数,其中m <= n
Select * from company where phone_number regexp ‘9{3}’;
Select * from company where phone_number regexp ‘9{2,3}’;