我有一个id和一个文本字段。
我需要搜索一个完全匹配的单词,或者在单个mysql查询的字符串中以单词开头。
E.g select * from tablename where textfield like "%cam%.
这将返回在单个位置找到cam的所有文本ID。
但是我需要通过在句子中分割单个单词来获得可以查询的结果。
id text
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
这两个查询无效 可以帮助一些人
由于
答案 0 :(得分:2)
当你在SQL查询中使用camel(正确)的情况时,字符串将找到字符串的区分大小写的匹配,但是,“camel”和“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”和“%”之间的空格)将返回2.
注意字符串大小写的区别。
答案 1 :(得分:0)
select * from tablename where textfield like "Camel%" or textfield like ' %Camel'
注意第二个条件中%
之前的空格。如果任何单词以字符串中的Camel
开头,则这两个条件将匹配,如果它是第一个单词则无关紧要。
答案 2 :(得分:0)
这适用于所有情况
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
答案 3 :(得分:0)
用户匹配
$ conditions [] = array(“MATCH(MODEL_name.fieldname)AGAINST('$ text'IN BOOLEAN MODE)”);