使用concat选择firstname和lastname搜索

时间:2013-04-09 10:04:00

标签: php mysql

这是我的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 个答案:

答案 0 :(得分: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']; ?>

答案 1 :(得分: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.'%" )';

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

答案 2 :(得分: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.'%" )';

答案 3 :(得分: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)
}