数据库
首页 > 数据库> > sql模糊查询

sql模糊查询

作者:互联网

SQL模糊查询

%:代表匹配0个字符、1个字符或者多个字符
_:代表匹配有且只有一个字符
[]:代表匹配范围内
[^]:代表匹配不在范围内

select * from people  where PeopleName  like '刘%';
--查询姓刘的员工,名字是两个字
select *from people where PeopleName like '刘_';
--取第几个字符,可以使用函数substring()
select *from people where SUBSTRING(PeopleName,1,1)='刘' and len(PeopleName)=2;
--取长度函数,len()
--查询名字最后一个字为香字,名字一共是三个字的员工信息
SELECT  * from people where SUBSTRING (PeopleName ,3,1)='香' and len(PeopleName )=3;
--查询员工电话号码,前三位是138开头的员工信息
select * from people WHERE peoplePhone like '138%';
--查询员工电话号码,前三位是138开头,第四位好像是7或8,最后一位是5的员工信息
    SELECT * from people where peoplePhone like '138[7,8]%5';
--查询员工电话号码,前三位是138开头,第四位好像是7或8,最后一位是不是2和3的员工信息
    SELECT * from people where peoplePhone like '138[2,3,4,5]%[^2,3]';

标签:like,people,--,模糊,查询,PeopleName,sql,138,where
来源: https://www.cnblogs.com/wherehappens/p/16426937.html