在字符串中搜索一个单词以查找完全匹配/甚至从单词不起作用开始


search a word in a string for exact match / even startingwith the word is not working

我有一个id和一个文本字段。

我需要搜索一个完全匹配的单词,或者用单个mysql查询以字符串中的单词开头。

例如select * from tablename where textfield like "%cam%.这将返回在单曲中任何位置都可以找到的所有文本id。

但我需要得到一个可以通过拆分一个句子中的单个单词来查询的结果。

id文本

1 Camel_walk.
2 camel does drink water.
3 does Camel_store water.
4 In deset people can find_camel
5 this will not return

当我查询select * from tablename where textfield like "%camel%.返回第一个1,2,3,4

但我需要得到单词以camel开头的id

1)select * from tablename where textfield like "Camel%"
return 1,3 ( which is not working for me)
or even
2)select * from tablename where textfield like "%Camel"
return 4

这两个查询不起作用有人能帮助吗

感谢

当您在类似SQL的查询中使用camel(正确的)大小写时,字符串将找到与字符串区分大小写的匹配,但是,"camel"answers"camel"将查找camel/camel/camel。。。依此类推(不区分大小写的匹配)

select id from tablename where text like "%Camel%"将返回1,3。

select id from tablename where text like "%camel"将返回4。

select id from tablename where text like "%camel%"将返回1,2,3,4。

select id from tablename where text like "Camel%"将返回1。

select id from tablename where text like "camel%"将返回1,2。

select id from tablename where text like "camel %"("camel"answers"%"之间的空格)将返回2。

请注意字符串大小写的差异。

select * from tablename where textfield like "Camel%" or textfield like ' %Camel'

注意第二个条件中%前面的空格。当字符串中的任何单词以Camel开头时,这两个条件都会匹配,不管它是否是第一个单词。

这应该适用于所有情况

SELECT id
  from tablename
 WHERE textfield like '%[^A-z^0-9]Camel%' -- In the middle of a sentence
    OR textfield like 'Camel%'            -- At the beginning of a sentence

用户与的匹配

$conditions[]=数组("MATCH(MODEL_name.fieldname)AGAINST('$text'IN BOOLEAN MODE)");