是否可以在mysql或PHP或CODEIGNITER中搜索显示多个可能结果的多个单词

时间:2013-11-26 07:21:55

标签: php mysql codeigniter

我在sql中有表

员工表

ID firstname  lastname      bandname
------------------------------------
 1 John       Lennon        Beatles
 2 Paul       Mccartney     Beatles
 3 Ringo      Starr         Beatles
 4 George     Harrison      Beatles

现在如果我搜索“George Mccartney”,结果应该是ID为4和2的行

或者如果搜索“1 Paul Starr”,结果应该是包含这三个单词的3行。

5 个答案:

答案 0 :(得分:3)

$search="1 Paul Starr";
list($id,$fn,$ln)=explode(" ",$search);

/*
 Sanitize these values or prepare the query. An example could be
 Remember, if you neither sanitize these values nor use prepared statements, its
 Dangerous!
*/


$query="SELECT * FROM yourTABLE 
        WHERE ID=$id
        OR firstname LIKE '%$fn%'
        OR lastname LIKE '%$ln%' ";

 // Or better use Prepared statements

修改

好的,所以他们可以按任何顺序?然后就可以了

$search="1 Paul Starr";
list($v1,$v2,$v3)=explode(" ",$search);

//Sanitize, Remember?


 $query="SELECT * FROM yourTABLE 
    WHERE ID IN ('$v1','$v2','$v3')
    OR firstname IN ('$v1','$v2','$v3')
    OR lastname IN ('$v1','$v2','$v3')";

答案 1 :(得分:3)

尝试使用FULLTEXT索引,如下所示:

ALTER TABLE employee ADD FULLTEXT INDEX (ID,firstname,lastname);

然后你可以做

SELECT ID,firstname,lastname    
FROM employee          
WHERE MATCH (ID,firstname,lastname) AGAINST ('1 Paul Starr')

看看Text Search across multiple fields MySQL

答案 2 :(得分:2)

可能是一个更好的方法,但在这里你有一个例子:

SELECT id, firstname, lastname 
FROM employee      
WHERE firstname = 'input_name' 
   OR lastname = 'input_lastname'

答案 3 :(得分:2)

$a="George Mccartney";
$name=explode(" ",$a);

$query="select * from employee
       where firstname like '%$name[0]%' 
        or 
       firstname like '%$name[1]%' 
       or lastname like '%$name[0]%' 
       or lastname like '%$name[1]%'
        ";

如果您不确定"1 Paul Starr"之类的用户输入,请执行以下操作:

 $a="George Mccartney hghh hghh ....whatever";
 $name=explode(" ",$a);
 $count=count($name);
   for($i=0;$i<$count;$i++)
   {
           $query="select * from employee
           where firstname like '%$name[i]%' 
            or 
           lastname like '%$name[i]%'
            or 
           id='$name[i]' ";

         -----your code to print the output or take the output in array----
   }

答案 4 :(得分:2)

sqlFiddle example

SET @search = '1 Paul Starr';

SELECT *
FROM employee
WHERE @search LIKE CONCAT('%',id,'%')
   OR @search LIKE CONCAT('%',firstname,'%')
   OR @search LIKE CONCAT('%',lastname,'%');

用我的PHP代码替换@search,因为我不熟悉PHP。

更新的答案:允许搜索以下搜索的子字符串:

“Geor”返回1排“乔治”
  “Geor Pau”返回2排“乔治”和“保罗” sqlFiddle example

SELECT *
FROM employee
WHERE 
          id LIKE CONCAT('%',SUBSTRING_INDEX(@search,' ',1),'%')
       OR id LIKE CONCAT('%',SUBSTRING_INDEX(SUBSTRING_INDEX(@search,' ',2),' ',-1),'%')
       OR id LIKE CONCAT('%',SUBSTRING_INDEX(SUBSTRING_INDEX(@search,' ',3),' ',-1),'%')
OR firstname LIKE CONCAT('%',SUBSTRING_INDEX(@search,' ',1),'%')
OR firstname LIKE CONCAT('%',SUBSTRING_INDEX(SUBSTRING_INDEX(@search,' ',2),' ',-1),'%')
OR firstname LIKE CONCAT('%',SUBSTRING_INDEX(SUBSTRING_INDEX(@search,' ',3),' ',-1),'%')
 OR lastname LIKE CONCAT('%',SUBSTRING_INDEX(@search,' ',1),'%')
 OR lastname LIKE CONCAT('%',SUBSTRING_INDEX(SUBSTRING_INDEX(@search,' ',2),' ',-1),'%')
 OR lastname LIKE CONCAT('%',SUBSTRING_INDEX(SUBSTRING_INDEX(@search,' ',3),' ',-1),'%')
;

或者如果您愿意,下面使用LOCATE(更短的查询)做同样的事情 sqlFiddle example

SELECT *
FROM employee
WHERE 
   LOCATE(SUBSTRING_INDEX(@search,' ',1)                        ,CONCAT(id,firstname,lastname))>0
OR LOCATE(SUBSTRING_INDEX(SUBSTRING_INDEX(@search,' ',2),' ',-1),CONCAT(id,firstname,lastname))>0
OR LOCATE(SUBSTRING_INDEX(SUBSTRING_INDEX(@search,' ',3),' ',-1),CONCAT(id,firstname,lastname))>0
;