首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在字符串中查找值的mysql查询

问在字符串中查找值的mysql查询
EN

Stack Overflow用户
提问于 2011-10-22 05:16:40
回答 3查看 61关注 0票数 1

我想知道我是否可以在我的查询中放一些东西来搜索字符串中的一个单词。例如,如果我有一个名为user_purchased的字段,我想查询它并只提取字符串中存在单词‘狗’的记录。

我知道我可以像这样使用php做这件事。

代码语言:javascript
复制
(!stristr($row['user_purchased'], 'dog'))

但是,我想知道是否可以在查询本身中执行此操作,这可能会提高性能。

事先感谢你的帮助

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-10-22 05:20:38

您可以使用LIKE:

代码语言:javascript
复制
WHERE your_col LIKE '%dog%'

如果你想要更好的性能,你可以看看full text search。它使用全文索引来快速查找匹配的行。

票数 1
EN

Stack Overflow用户

发布于 2011-10-22 05:20:03

在MySql中,您可以使用INSTR(str,substr)

引用http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr上的MySql手册

代码语言:javascript
复制
INSTR(str,substr)

Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed.

mysql> SELECT INSTR('foobarbar', 'bar');
        -> 4
mysql> SELECT INSTR('xbar', 'foobar');
        -> 0
This function is multi-byte safe, and is case sensitive only if at least one argument is a binary string.
票数 0
EN

Stack Overflow用户

发布于 2011-10-22 05:21:05

SELECT * FROM table WHERE user_purchased LIKE '%dog%';

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7855360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档