Home  >  Article  >  Database  >  mysql中的like关键字

mysql中的like关键字

WBOY
WBOYOriginal
2016-06-07 15:20:401693browse

MySQL中的LIKE关键字与匹配模式联合使用,用于确定所有与给定匹配模式相匹配的记录。匹配模式类与正则表达式,它本身是一个字符串,但是这个字符串中的字符描述了某种句法规则,也就是说,这些字符串描述了某类字符串必须满足的规则。匹配模式中可以包括常规

MySQL中的LIKE关键字与匹配模式联合使用,用于确定所有与给定匹配模式相匹配的记录。匹配模式类似与正则表达式,它本身是一个字符串,但是这个字符串中的字符描述了某种句法规则,也就是说,这些字符串描述了某类字符串必须满足的规则。匹配模式中可以包括常规字符与通配符。MySQL支持的通配符及意义如下:

通配符 含义
% 表示零个或者多个任意字符,例如“s%”表示以s开头的任意字符串,“%s%”表示包含字符s的任意字符
_(下划线) 表示任意单个字符,例如“sm_”表示任何以“sm”开头的长度为3的字符串,支持中文,可以对应任意一个中文字符

如果匹配模式中没有包含通配符,则与使用“=”的运算效果是相同的,例如:

<ol><li><span><span>select</span><span> * </span><span>from</span><span> grade </span><span>where</span><span> </span><span>no</span><span> </span><span>like</span><span> </span><span>'110101'</span><span>; </span></span></li></ol>

等价于

<ol><li><span><span>select</span><span> * </span><span>from</span><span> grade </span><span>where</span><span> </span><span>no</span><span> = </span><span>'110101'</span><span>; </span></span></li></ol>

下面来看看几个小实例:

<ol>
<li><span><span>-- 查询所有姓“许”的学生</span><span> </span></span></li>
<li><span><span>select</span><span> * </span><span>from</span><span> student </span><span>where</span><span> </span><span>name</span><span> </span><span>like</span><span> </span><span>'许%'</span><span>; </span></span></li>
</ol>
<ol>
<li><span><span>-- 查询所有姓“许”,且名字全称只有两个字的学生</span><span> </span></span></li>
<li><span><span>select</span><span> * </span><span>from</span><span> student </span><span>where</span><span> </span><span>name</span><span> </span><span>like</span><span> </span><span>'许_'</span><span>; <br></span></span></li>
</ol>
<ol>
<li><span><span>-- 查询email地址是以x开头的sohu.com的邮箱的学生</span><span> </span></span></li>
<li><span><span>select</span><span> * </span><span>from</span><span> mail </span><span>like</span><span> </span><span>'x%@sohu.com'</span><span>; </span></span></li>
</ol>

可以将NOT关键字与LIKE同时使用,例如查询所有不姓“许”的学生:

<ol><li><span><span>select</span><span> * </span><span>from</span><span> student </span><span>where</span><span> </span><span>name</span><span> </span><span>not</span><span> </span><span>like</span><span> </span><span>'许%'</span><span>; </span></span></li></ol>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn