mysql like escape_mysql like语句用法详解 含通配符使用方法

mysql like语句用法详解 含通配符使用方法2017-11-07 20:46

在对数据进行模糊查询的时候,我们基本上都会用到like语句。

我们先来了解一下like语句中的通配符:百分号(%)、下划线(_)和escape

%:表示任意个或多个字符,可匹配任意类型和长度的字符。

select * from article where content like '%mysql教程%';

搜索包含“mysql教程”的内容。

select * from article where content like '%mysql教程';

搜索以“mysql教程”结尾的内容。

select * from article where content like 'mysql教程%';

搜索以“mysql教程”开头的内容。

如果有一段话为“php是最好的编程语言”

想搜索包含“php”和"编程"该用什么语句呢?

首先想到的可能是如下写法:

select * from article where content like '%php%' and content like '%编程%';

其实可以这样写:

select * from article where content like '%php%编程%';

_:表示任意单个字符,匹配单个任意字符,它常用来限制表达式的字符长度语句(可以代表一个中文字符)

select * from article where title like '_';

select * from article where title like 'php_';

select * from article where title like 'p_php';

如果我们要搜索的内容就是要包含有“%”或“_”怎么办呢?

使用escape,转义字符“/”后面的%或_就不再作为通配符使用了,注意前面没有转义字符的%和_仍然起通配符作用。

select * from article WHERE title like '%mysql/_%' ESCAPE '/';

select * from article WHERE title like '%mysql/%%' ESCAPE '/';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值