网站首页 博客 MySQL判断是否包含某个字符串
MySQL判断是否包含某个字符串
 2017-05-12 16:17:15  管理员  221

like语句

s.e.l.e.c.t * from `table` w.h.e.r.e `condition` like `%keyword%';

事实上,可以使用 locate(position) 和 instr 这两个函数来代替

一、locate语句

s.e.l.e.c.t * from `table` w.h.e.r.e locate(‘keyword’, `condition`)>0;

二、或是 locate 的別名 position

position语句

s.e.l.e.c.t * from `table` w.h.e.r.e position(‘keyword’ in `condition`);

三、instr语句

s.e.l.e.c.t * from `table` w.h.e.r.e instr(`condition`, ‘keyword’)>0;

locate、position 和 instr 的差別只是参数的位置不同,同时locate 多一个起始位置的参数外,两者是一样的。

四、find_in_set函数

find_in_set(str1,str2) 返回str2中str1所在的位置索引,其中str2必须以","分割开。

例如test表结构

idname
1zhangsan,lisi
2wangwu,liuliu

查询name中含有zhangsan的

s.e.l.e.c.t * from test w.h.e.r.e find_in_set('zhangsan',name);

来说两句吧
最新评论