[英]FULLTEXT Search Multiple Columns in MySQL?
So I've been struggling with this for the last couple days.. I have music genres.所以过去几天我一直在努力解决这个问题..我有音乐流派。 Separated by primary and secondary columns.
由主列和辅助列分隔。 Rock / Progressive rock, etc. And i have it so it'll search one column, just fine.
摇滚/前卫摇滚等。我有它,所以它会搜索一栏,就好了。 But i'd like it to search either column.
但我希望它搜索任一列。 searching for help on google.
在谷歌上寻找帮助。 I found that a FULLTEXT search in theory should do this.
我发现理论上 FULLTEXT 搜索应该这样做。 I setup the FULLTEXT index on both the Primary and secondary columns.. But I haven't been able to get it working.
我在主列和辅助列上都设置了 FULLTEXT 索引。但我一直无法让它工作。
this is my code that works for the single column:这是我的适用于单列的代码:
$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;
(It returns the results in a JSON format) (它以 JSON 格式返回结果)
I've tried to follow this tutorial i've found that gives this as an example:我尝试按照本教程进行操作,我发现它以此为例:
$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);
the question seems to be either in the SELECT query.问题似乎在 SELECT 查询中。 or when i do combine it.
或者当我把它结合起来时。 it causes an error with this line:
它会导致此行出错:
while($obj = mysql_fetch_object($rs)) {
the most recent Select query i tried was:我最近尝试的 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";
I've also tried with: LIKE '%%%s%%' (which would be more useful. as partial matches would be nice)我也试过: LIKE '%%%s%%' (这会更有用。因为部分匹配会很好)
I'm not 100% sure what you are looking for but if you want to search multiple columns something like this should work:我不是 100% 确定你在找什么,但如果你想搜索多个列,这样的东西应该可以工作:
"SELECT * FROM musicgenres WHERE secondary LIKE '%%%s%%' OR primary LIKE '%%%s%%' LIMIT 20"
Hope that's close to what you are looking for.希望这接近您正在寻找的内容。
Maybe you want to add indexes that enclose the columns you want to search:也许您想添加包含要搜索的列的索引:
ALTER TABLE `table`
ADD FULLTEXT `fulltext_idxAll` (`column1`, `column2`,`column3`);
SELECT * FROM `table`
WHERE MATCH (`column1`, `column2`,`column3`)
AGAINST ('whateveryouwanttosearch')
hope this is useful for you.希望这对你有用。
Ok try this query好的,试试这个查询
SELECT column1, column2, ..., MATCH(primary, secondary) AGAINST('". $term ."') as score
FROM musicgenres
WHERE MATCH (primary, secondary) AGAINST('". $term ."')
ORDER BY score DESC"
If it dosent work try to remove match() against() from select clause如果它不起作用,请尝试从 select 子句中删除 match() against()
SELECT column1, column2, ... FROM ....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.