[英]php mysql search query wildcard problem
If I search for LENA, I only want to get results where LENA is one word and not part of a word like Lenason or Anna-Lena or Malena 如果我搜索LENA,则只想获得LENA是一个单词而不是Lenason或Anna-Lena或Malena之类单词的一部分的结果
How do I write such a query, not 我怎么写这样的查询,不是
"select * from users where name like '%LENA%'"
What should it be instead? 应该是什么呢?
Use this one: 使用这个:
select * from users where name like '% LENA %' OR name like 'LENA %' OR name like '% LENA' OR name like 'LENA'
Try it, it will work, and too simple with 2 spaces at start and end. 尝试一下,它会起作用,并且太简单了,在开始和结束处只有两个空格。 Its long but fast query. 它长而快速的查询。 You can also use Full Text Search, Concat, Regex ... but for me, I like simple, just add some space before %. 您也可以使用全文本搜索,Concat,Regex ... ...但是对我来说,我喜欢简单,只需在%之前添加一些空格即可。
However, you can try shorter but slower query: 但是,您可以尝试使用较短但较慢的查询:
SELECT * FROM users WHERE name REGEXP '(^|\s+)LENA($|\s+)'
You could use REGEXP
: 您可以使用REGEXP
:
SELECT fields
FROM users
WHERE name REGEXP '(^|\s+)LENA($|\s+)'
You might be better off looking into Full Text Search for this. 您可能最好选择全文搜索 。
Otherwise I think you're stuck doing something like this 否则我认为你被困在做这样的事情
"select * from users WHERE CONCAT(' ',name, ' ') like '% LENA %'"
Which will be pretty inefficient as it requires a full table scan. 由于需要全表扫描,因此效率很低。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.