[英]FULLTEXT Search Multiple Columns in MySQL?
所以過去幾天我一直在努力解決這個問題..我有音樂流派。 由主列和輔助列分隔。 搖滾/前衛搖滾等。我有它,所以它會搜索一欄,就好了。 但我希望它搜索任一列。 在谷歌上尋找幫助。 我發現理論上 FULLTEXT 搜索應該這樣做。 我在主列和輔助列上都設置了 FULLTEXT 索引。但我一直無法讓它工作。
這是我的適用於單列的代碼:
$query = sprintf("SELECT * FROM musicgenres WHERE secondary LIKE '%%%s%%' LIMIT 20", mysql_real_escape_string($_GET["q"]));
$arr = array();
$rs = mysql_query($query);
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
$json_response = json_encode($arr);
if($_GET["callback"]) {
$json_response = $_GET["callback"] . "(" . $json_response . ")";
}
$final = str_replace(genre_id, id, $json_response);
echo $final;
(它以 JSON 格式返回結果)
我嘗試按照本教程進行操作,我發現它以此為例:
$term = "Search Term";
$sql = "SELECT *, MATCH(title, content) AGAINST('". $term ."') as score FROM pages WHERE MATCH (title, content) AGAINST('". $term ."') ORDER BY score DESC";
$query = mysql_query($sql);
問題似乎在 SELECT 查詢中。 或者當我把它結合起來時。 它會導致此行出錯:
while($obj = mysql_fetch_object($rs)) {
我最近嘗試的 Select 查詢是:
$term = mysql_real_escape_string($_GET["q"]);
$sql = "SELECT *, MATCH(primary, secondary) AGAINST('". $term ."') as score FROM musicgenres WHERE MATCH (primary, secondary) AGAINST('". $term ."') ORDER BY score DESC";
我也試過: LIKE '%%%s%%' (這會更有用。因為部分匹配會很好)
我不是 100% 確定你在找什么,但如果你想搜索多個列,這樣的東西應該可以工作:
"SELECT * FROM musicgenres WHERE secondary LIKE '%%%s%%' OR primary LIKE '%%%s%%' LIMIT 20"
希望這接近您正在尋找的內容。
也許您想添加包含要搜索的列的索引:
ALTER TABLE `table`
ADD FULLTEXT `fulltext_idxAll` (`column1`, `column2`,`column3`);
SELECT * FROM `table`
WHERE MATCH (`column1`, `column2`,`column3`)
AGAINST ('whateveryouwanttosearch')
希望這對你有用。
好的,試試這個查詢
SELECT column1, column2, ..., MATCH(primary, secondary) AGAINST('". $term ."') as score
FROM musicgenres
WHERE MATCH (primary, secondary) AGAINST('". $term ."')
ORDER BY score DESC"
如果它不起作用,請嘗試從 select 子句中刪除 match() against()
SELECT column1, column2, ... FROM ....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.