分裂斷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)
}
這是什麼問題? – 2013-04-09 10:08:00
所以你會得到任何錯誤? – 2013-04-09 10:08:04
好吧,是否有任何錯誤或任何...因爲我認爲這將工作得很好.. – 2013-04-09 10:09:00