MySql的like语句中的通配符:百分号、下划线和escape
2016-07-22 13:35
567 查看
MySql的like语句中的通配符:百分号、下划线和escape
%:表示任意个或多个字符。可匹配任意类型和长度的字符。
Sql代码
select * from user where username like '%huxiao';
select * from user where username like 'huxiao%';
select * from user where username like '%huxiao%';
另外,如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件
SELECT * FROM [user] WHERE u_name LIKE ‘%三%’ AND u_name LIKE ‘%猫%’
若使用 SELECT * FROM [user] WHERE u_name LIKE ‘%三%猫%’
虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”。
_:表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:(可以代表一个中文字符)
Sql代码
select * from user where username like '_';
select * from user where username like 'huxia_';
select * from user where username like 'h_xiao';
如果我就真的要查%或者_,怎么办呢?使用escape,转义字符后面的%或_就不作为通配符了,注意前面没有转义字符的%和_仍然起通配符作用
Sql代码
select username from gg_user where username like '%xiao/_%' escape '/';
select username from gg_user where username like '%xiao/%%' escape '/';
%:表示任意个或多个字符。可匹配任意类型和长度的字符。
Sql代码
select * from user where username like '%huxiao';
select * from user where username like 'huxiao%';
select * from user where username like '%huxiao%';
另外,如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件
SELECT * FROM [user] WHERE u_name LIKE ‘%三%’ AND u_name LIKE ‘%猫%’
若使用 SELECT * FROM [user] WHERE u_name LIKE ‘%三%猫%’
虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”。
_:表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:(可以代表一个中文字符)
Sql代码
select * from user where username like '_';
select * from user where username like 'huxia_';
select * from user where username like 'h_xiao';
如果我就真的要查%或者_,怎么办呢?使用escape,转义字符后面的%或_就不作为通配符了,注意前面没有转义字符的%和_仍然起通配符作用
Sql代码
select username from gg_user where username like '%xiao/_%' escape '/';
select username from gg_user where username like '%xiao/%%' escape '/';
相关文章推荐
- MYSQL的随机查询的实现方法 (未测试)
- Mysql计算时间差函数
- win7安装mysql绿色压缩版
- MySQL单表多字段模糊查询
- mysql orderby limit 翻页数据重复的问题
- MySql 按记录查询使用distinct去重的小技巧
- mysql分区
- 关于链接mysql报MySQLSyntaxErrorException: Table ** doesn't exist的问题
- show process in mysql
- MyBatis操作mysql配置和获取插入记录的自增主键
- MyBatis操作mysql配置和获取插入记录的自增主键
- MyBatis操作mysql配置和获取插入记录的自增主键
- MyBatis操作mysql配置和获取插入记录的自增主键
- MyBatis操作mysql配置和获取插入记录的自增主键
- mysql-proxy 读写分离
- MySQL线程池总结
- 让MySQL支持Emoji表情
- MySQL 性能调优的10个方法
- MySQL 5.6 设置慢日志, 无法启动
- MySQL5.6之Index Condition Pushdown(ICP,索引条件下推)