2012-08-10 78 views
0

我正在构建一个Mysql全文搜索,我有4个表来搜索。当我在搜索查询中只使用单个表时,它可以工作,但是当我在查询中使用多个表时,它不起作用。我也不想在查询中使用UNION ALL,因为我使用复选框选择选项来执行单个或多个表中的搜索。Mysql全文搜索多表的查询修改

MySQL查询多表不工作。

$sqlquery = mysql_query("(SELECT * FROM table1, table2, table3, table4 WHERE MATCH (pflink, title) AGAINST ('%$keyword*%' IN BOOLEAN MODE))ORDER by pflink desc, $orderby $sortby LIMIT $rowsperpage OFFSET $offset ")or die (mysql_error()); 

它给了我错误Column 'pflink' in where clause is ambiguous

使用单表查询工作。

$sqlquery = mysql_query("(SELECT * FROM table1 WHERE MATCH (pflink, title) AGAINST ('%$keyword*%' IN BOOLEAN MODE))ORDER by pflink desc, $orderby $sortby LIMIT $rowsperpage OFFSET $offset ")or die (mysql_error()); 

的Html代码复选框

<input name="all-tables" type="checkbox" value="true" checked="checked" id="check-all" > 
    <input name="table1" type="checkbox" value="true" disabled="disabled" > 
    <input name="table2" type="checkbox" value="true" disabled="disabled" > 
    <input name="table3" type="checkbox" value="true" disabled="disabled" > 
    <input name="table4" type="checkbox" value="true" disabled="disabled" > 

请提出任何可能的方式来修改这个查询,使之成为多个表工作。

谢谢。

+0

,因为你的mysql引擎发生了混乱,为这台pflink属于:) – WatsMyName 2012-08-10 08:35:17

+0

@Sabin pflink和标题中的所有表,其实所有的表结构相同,但不同的值。 – 2012-08-10 08:36:27

+0

我希望你已经在每个表格上定义了索引。同样,如果您拥有所有具有相同结构的表,pflink仍然不明确,因为您尚未定义pflink引用的表。此外,到目前为止,我知道,全文搜索将无法在多个表中使用。对于类似的讨论http://stackoverflow.com/questions/1241602/mysql-match-across-multiple-tables – WatsMyName 2012-08-10 08:48:31

回答

0

很久以前我回到类似的条件。我有多个表格来使用全文,但我无法弄清楚我想做什么的方式。我搜查,但无法找到方式。所以最后我想出了以下想法

1. Make another table say "keywords", with fields title, description , item_id, and the table data belongs to. 
2. make a code to fetch all title and description from table1,table2 and so on and insert it to keywords. 
3. Make keywords table suitable for full text search. 
4. when user searches, search keyword table. Retrieve relevant value from table1, table 2 etc from the recordset (as you get item id and table from the full text search query). 

这样做的好处是查询会更快,更容易。

希望这有助于

+0

您的解决方案就像制作一个包含所有表格数据的表格,然后进行全文搜索。 – 2012-08-10 09:35:27

+0

是的,我有不同的表,相似的结构,但不同的价值。第一张表包含与衣服相关的价值,第二个涉及鞋子,第三个涉及化妆品,第四个涉及书籍。我有一个像谷歌一样的文本框,搜索时我必须显示所有表的结果。我发现这种方式影响和更容易..事实上,这只是我想出的解决方案。我编写了一个cron作业来截断关键字表,并从所有表中选择所有值并将其重新插入到关键字表中。您也可以偶尔手动运行此代码。 – WatsMyName 2012-08-10 09:39:44

+0

你知道一种解决单个搜索查询的方法吗?它可以检查类别,比如'WHERE MATCH(pflink = somecategory,title)' – 2012-08-10 10:05:05