开发者

MySQL search a text from two columns

i have a table (tbl_world) which look like this

  id | first_name | last_name | age | class |

now i want to search the text which can be anywhere in first_name or in last_name

i m using below mysql query

 "SELECT * FROM tbl_world WHERE REG开发者_如何学编程EXP '".$word."' IN( first_name, last_name)";

where $word is user input (means if i search 'hell' then 'hello' as well as 'wellhell' also returned in result)

above query display error, please suggest me optimize method for search in mysql.

addition question: should i use LIKR or RLIKE?


I would do it with:

"SELECT * FROM tbl_world WHERE first_name REGEXP '" . $word .
    "' OR  last_name REGEXP '" . $word . "'"


 "SELECT * FROM tbl_world 
WHERE first_name LIKE  '%".$word."%' 
OR last_name LIKE  '%".$word."%'";

(Consider enabling fulltext search if you want to search for a string among multiple fields.)


"SELECT * FROM tbl_world WHERE first_name LIKE '%'".$word."%' OR last_name LIKE '%".$word."%'"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜