MySQL 从两列中搜索文本 [英] MySQL search a text from two columns
本文介绍了MySQL 从两列中搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的表(tbl_world)
i have a table (tbl_world) which look like this
id | first_name | last_name | age | class |
现在我想搜索可以在 first_name 或 last_name 中任何位置的文本
now i want to search the text which can be anywhere in first_name or in last_name
我正在使用下面的 mysql 查询
i m using below mysql query
"SELECT * FROM tbl_world WHERE REGEXP '".$word."' IN( first_name, last_name)";
其中 $word
是用户输入(意味着如果我搜索 'hell' 然后 'hello' 以及 'wellhell' 也在结果中返回)
where $word
is user input (means if i search 'hell' then 'hello' as well as 'wellhell' also returned in result)
以上查询显示错误,请建议我在mysql中优化搜索方法.
above query display error, please suggest me optimize method for search in mysql.
附加问题:我应该使用 LIKR 还是 RLIKE?
addition question: should i use LIKR or RLIKE?
推荐答案
我会这样做:
"SELECT * FROM tbl_world WHERE first_name REGEXP '" . $word .
"' OR last_name REGEXP '" . $word . "'"
这篇关于MySQL 从两列中搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文