MySQL全文搜索,为什么我得到错误的MATCH参数

时间:2010-04-09 20:47:52

标签: mysql

SELECT 
* 
FROM 
company c
INNER JOIN 
city ci ON ci.city_id = c.city_id
INNER JOIN 
state s ON s.state_id = c.state_id
WHERE 
MATCH (
c.name, ci.name, c.zipcode, s.name
)
AGAINST (
'los angeles'
)

2 个答案:

答案 0 :(得分:12)

MATCH (...)中的所有列必须来自同一个表,因为它们必须是全文索引的,并且MySQL无法为多个表中的数据创建一个索引。

答案 1 :(得分:8)

我遇到了同样的问题并解决了这个问题:

SELECT 
* 
FROM 
company c
INNER JOIN 
city ci ON ci.city_id = c.city_id
INNER JOIN 
state s ON s.state_id = c.state_id
WHERE 
   MATCH (c.name) AGAINST ('los angeles')
OR MATCH (ci.name) AGAINST ('los angeles')
OR MATCH (c.zipcode) AGAINST ('los angeles')
OR MATCH (s.name) AGAINST ('los angeles')

但正如我所见,您正在搜索“name”和“zipcode”等简单字段。至于我,最好使用LIKE并将它们连接起来

SELECT 
* 
FROM 
company c
INNER JOIN 
city ci ON ci.city_id = c.city_id
INNER JOIN 
state s ON s.state_id = c.state_id
WHERE 
CONCAT_WS (' ', c.name, ci.name, c.zipcode, s.name)
LIKE ('%los angeles%')