2013-04-09 76 views
-2

這是我的sql查詢。我需要使用搜索來搜索名字和姓氏。使用concat選擇名和姓氏搜索

$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.'%")'; 
+0

這是什麼問題? – 2013-04-09 10:08:00

+0

所以你會得到任何錯誤? – 2013-04-09 10:08:04

+0

好吧,是否有任何錯誤或任何...因爲我認爲這將工作得很好.. – 2013-04-09 10:09:00

回答

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.'%")'; 

編輯:根據您的意見更新查詢

+0

ok Thankyou suresh .hope這對我很有用 – 2013-04-09 10:12:46

+0

so raheel你能告訴我正確的方式嗎? – 2013-04-09 10:15:09

+0

當我把全名搜索例如(約翰馬丁)它不給任何結果給我:( – 2013-04-09 10:21:37

2

結果取爲簡單的查詢和使用concatination顯示結果...

$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']; ?> 
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.'%")'; 
0

分裂斷OR值到不同的聯合在一起的查詢應該允許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) 
}