多个表的Mysql全文搜索查询修改

时间:2012-08-10 08:28:17

标签: php mysql html search

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

多个表上的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" >

请建议任何可能的方法来修改此查询,使其适用于多个表。

感谢。

1 个答案:

答案 0 :(得分: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).

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

希望这有帮助