-3

这是我的 sql 查询。我需要使用搜索来搜索名字和姓氏。

$result =  'SELECT * FROM resume 
            WHERE   (first_name LIKE "'.$sKeyword.'%" OR 
                     last_name LIKE "'.$sKeyword.'%" OR
                     CONCAT(first_name," ",last_name) like "'.$sKeyword.'%" OR 
                     email LIKE "'.$sKeyword.'%" OR 
                     phone LIKE "'.$sKeyword.'%" OR 
                     zip_code LIKE "'.$sKeyword.'%" OR 
                     find_us LIKE "'.$sKeyword.'%" )';
4

4 回答 4

2

获取结果作为简单查询并使用连接显示结果...

$seresult='SELECT * FROM resume 
            WHERE   (first_name LIKE "%'.$sKeyword.'%" OR 
                     last_name LIKE "%'.$sKeyword.'%" OR
                     email LIKE "%'.$sKeyword.'%" OR 
                     phone LIKE "%'.$sKeyword.'%" OR 
                     zip_code LIKE "%'.$sKeyword.'%" OR 
                     find_us LIKE "%'.$sKeyword.'%" )';

在显示之后

<?= $seresult['first_name']; ?>-<?= $seresult['last_name']; ?>
于 2013-04-09T10:15:24.897 回答
0

在运行查询之前,只需检查表中是否存在数据。

试试下面的方法:

$fullName = "john martin";
$nameArray = explode(" ",$fullName);
if(count($nameArray)>1)
  $search = " first_name in (".implode(',',$nameArray).") OR last_name in (".implode(',',$nameArray).")";
else
  $search = " first_name LIKE "%'.$sKeyword.'%" OR last_name LIKE "%'.$sKeyword.'%" OR"

$seresult='SELECT * FROM resume 
            WHERE   ('.$search.'
                     email LIKE "%'.$sKeyword.'%" OR 
                     phone LIKE "%'.$sKeyword.'%" OR 
                     zip_code LIKE "%'.$sKeyword.'%" OR 
                     find_us LIKE "%'.$sKeyword.'%" )';

编辑:根据您的评论更新了查询

于 2013-04-09T10:11:13.263 回答
0

将 OR 拆分为不同的 UNIONed 查询应该允许 MySQL 更有效地使用其索引。

我假设您正在寻找仅以输入的短语开头的项目。如果是这样,这可能会有所帮助,因为如果搜索短语中有空格,它只会对 first_name 和 last_name 进行组合搜索。

$result =  "SELECT * FROM resume 
            WHERE   first_name LIKE '$sKeyword%' 
            UNION
            SELECT * FROM resume 
            WHERE   last_name LIKE '$sKeyword%' 
            UNION
            SELECT * FROM resume 
            WHERE   email LIKE '$sKeyword%'
            UNION
            SELECT * FROM resume 
            WHERE   phone LIKE '$sKeyword%'
            UNION
            SELECT * FROM resume 
            WHERE   zip_code LIKE '$sKeyword%'
            UNION
            SELECT * FROM resume 
            WHERE   find_us LIKE '$sKeyword%'
            UNION
            SELECT * FROM resume 
            WHERE   CONCAT(first_name,' ',last_name) like '$sKeyword%'";

$sKeywordArray = explode(' ', $sKeyword);
IF (count($sKeywordArray) > 1)
{
    $result2 = array();     
    foreach($sKeywordArray $value)
    {
        $result2[] = "(first_name like '$value%' OR last_name like '$value%')";
    }
    $result .= " UNION SELECT * FROM resume WHERE ".implode(' AND ', $result2)
}
于 2013-04-09T10:33:47.340 回答
0

尝试这个

      $seresult='SELECT * FROM resume 
            WHERE   (first_name LIKE "%'.$sKeyword.'%" OR 
                             first_name LIKE "'.$sKeyword.'%" OR 
                             first_name LIKE "%'.$sKeyword.'" OR 
                            last_name LIKE "%'.$sKeyword.'%" OR
                            last_name LIKE "'.$sKeyword.'%" OR
                            last_name LIKE "%'.$sKeyword.'" OR
                            email LIKE "%'.$sKeyword.'%" OR 
                            phone LIKE "%'.$sKeyword.'%" OR 
                            zip_code LIKE "%'.$sKeyword.'%" OR 
                            find_us LIKE "%'.$sKeyword.'%" )';
于 2013-04-09T10:17:30.250 回答