2016-12-01 58 views
0

如何搜索多個字段,這個代碼搜索單個列,請定製我的代碼如何在MySQL查詢中搜索多個列?

$keywords= "search keywords"; 
$keywords= htmlspecialchars($keywords);  
$keywords= mysqli_real_escape_string($con, $keywords); 
$countSql_s = "SELECT *, MATCH(website) AGAINST ('%".$keywords."%') AS score FROM search WHERE MATCH(website) AGAINST ('%".$keywords."%') ORDER BY score DESC"; 

回答

0

只要改變你的「WHERE」包括爲每列的檢查,就像這樣:

... WHERE MATCH(website) AGAINST('%".$keywords."%') > 5 
AND MATCH(username) AGAINST('%".$keywords."%') > 5 
... 
+0

感謝@傑里米·格爾 –

0

你需要改變你的表:

ALTER TABLE --table name-- ADD FULLTEXT fulltext_search (column1,colum2,column3); 

然後你就可以搜索這樣的:

MATCH (column1,column2) AGAINST ('%".$keywords."%') 
+0

我有小疑問是什麼意思fulltext_search @傑里米·格爾 –

+0

FULLTEXT是指數search.To使用全文需要定義自己的列FULLTEXT –

+0

感謝@ mr.Jeremy格爾 –