Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL full text search, why am I getting Incorrect arguments to MATCH

Tags:

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'
)
like image 945
vick Avatar asked Apr 09 '10 20:04

vick


People also ask

How to do full-text search in MySQL?

The basic query format of full-text searches in MySQL should be similar to the following: SELECT * FROM table WHERE MATCH(column) AGAINST(“string” IN NATURAL LANGUAGE MODE); When MATCH() is used together with a WHERE clause, the rows are automatically sorted by the highest relevance first.

What is full-text index in MySQL?

Full-text indexes are created on text-based columns ( CHAR , VARCHAR , or TEXT columns) to speed up queries and DML operations on data contained within those columns. A full-text index is defined as part of a CREATE TABLE statement or added to an existing table using ALTER TABLE or CREATE INDEX .


2 Answers

All columns in MATCH (...) must be from the same table, because they must be fulltext indexed, and MySQL cannot create one index for data in multiple tables.

like image 164
Messa Avatar answered Sep 22 '22 23:09

Messa


I had the same problem and solved it like this:

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')

But as I see, You are searching in simple fields like 'name' and 'zipcode'. As for me, it would be better to use LIKE and just concatenate them

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%')
like image 41
Karfax Avatar answered Sep 21 '22 23:09

Karfax