我想知道我是否可以在我的查询中放一些东西来搜索字符串中的一个单词。例如,如果我有一个名为user_purchased的字段,我想查询它并只提取字符串中存在单词‘狗’的记录。
我知道我可以像这样使用php做这件事。
(!stristr($row['user_purchased'], 'dog'))但是,我想知道是否可以在查询本身中执行此操作,这可能会提高性能。
事先感谢你的帮助
发布于 2011-10-22 05:20:38
发布于 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手册
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.发布于 2011-10-22 05:21:05
SELECT * FROM table WHERE user_purchased LIKE '%dog%';
https://stackoverflow.com/questions/7855360
复制相似问题