Like中的通配符包含有:
通配符Description示例
1.%占位符
表示0个或多个字符,可放在查询条件的任意位置
实例:
select * from table1 where code like ‘%abc’;查询列值以abc结尾的
Select * from table1 where code like ‘abc%’查询列值以abc开头的
Select * from table1 where code like ‘%abc%’查询列值包含abc的;
Select * from table1 where code like ‘ab%c’查询列值以ab开头,c结尾的
2._的用法
_占位符表示一个字符,可放在查询条件的任意位置用法和%类似
实例:
Select * from table1 where code like ‘abc_’;查询列值为abc开头的任意的四个字符
Select * from table1 where code like ‘_abc’;查询列值为abc 结尾的任意四个字符
Select * from table1 where code like ‘a_b_c’;查询列为为a?b?c的任务五个字符
Select * from table1 where code like ‘__abc’;查询列值未abc结尾的任务五个字符
3.正则匹配模式
通过正则表达式来模糊匹配,
‘.’通配符,占位符,表示任意一个字符,类似 _
mysql语法示例:
Select * from table1 where code rlike ‘abc.’;查询列值中包含abc?的任意字符这个查询结果和like ‘abc%’相比较,他要求是abc后面至少要有一个字符,而后者,不需要也可以直接是abc。查询效果和like ‘abc_%’ 一样,至少需要4个字符。
Select * from table1 where code rlike ‘abc..’查询列值包含abc??的任意字符,这里至少有五个字符
Oracle示例:
Select * from table1 where regexp_like(code,’abc.’);
Select * from table1 where regexp_like(code,’abc..’);
‘[ ]’通配符 使用方法同正则例如[0-9],表示0到9的任意数组,[a-z]表示a-z的任意字母
mysql语法示例
Select * from table1 wehre code rlike ‘abc[a-z]’查询列值为abc+任意一个小写字母的字符
Oracle语法示例
Select * from table1 where regexp_like(code,’abc[a-z]’);
‘^’通配符,表示非例如
mysql语法示例
Select * from table1 wehre code rlike ‘abc[^a-z]’查询列值为abc+任意一个不是小写字母的字符
Oracle语法示例
Select * from table1 where regexp_like(code,’abc[^a-z]’);
‘{ }’通配符,表示占位符的位数
mysql语法示例
Select * from table1 wehre code rlike ‘abc[a-z]{2}’查询列值为abc+任意2个小写字母的字符
Oracle语法示例
Select * from table1 where regexp_like(code,’abc[a-z]{2}’);
。。这里举例简单的一些,更多高级组合,配合正则表达式使用。
如果我们的列里面有一个特殊字符怎么办呢?比如列里面有_如果用是like会变成正占位符,
这里需要使用escape转义
示例
Select * from table1 where code like ‘abc%_’ escape ‘%’;这里的%不在是占位通配符了。而是需要匹配abc%开头的任意五个字符
来自:Sql中的like用法