简体   繁体   中英

How to use mysql FULLTEXT Search with WHERE in multiple fields using OR?

I am using mysql FULLTEXT Search, innodb table engine.
search term : tom

SELECT * FROM content WHERE MATCH(title) AGAINST('tom' IN BOOLEAN MODE) AND is_enabled = 1  AND category = 100 OR category = 200 ORDER BY seeders DESC LIMIT 0, 30

above query gives 550 records, expected records are 6

but if i search for each category separately. searching in category 100

SELECT * FROM content WHERE MATCH(title) AGAINST('tom' IN BOOLEAN MODE) AND is_enabled = 1  AND category = 100 ORDER BY seeders DESC LIMIT 0, 30

gives 5 records

and searching in category 200

SELECT * FROM content WHERE MATCH(title) AGAINST('tom' IN BOOLEAN MODE) AND is_enabled = 1  AND category = 200 ORDER BY seeders DESC LIMIT 0, 30

gives 1 record.

so i am expecting records 6 , by combining 5+1 but i am getting 550, even those records which doesn't have word tom in it.


Another Issue, is searching in category or sub_category in same query for word tom

SELECT * FROM content WHERE MATCH(title) AGAINST('tom' IN BOOLEAN MODE) AND is_enabled = 1  AND sub_category = 101 OR category = 200 ORDER BY seeders DESC LIMIT 0, 30

gives 550 records. expected records are 6

so how can i fix this ?

i have found solution. the query should be

SELECT * FROM content WHERE MATCH(title) AGAINST('tom' IN BOOLEAN MODE) AND is_enabled = 1  AND (category = 100 OR category = 200) ORDER BY seeders DESC LIMIT 0, 30

notice, the OR operator is in brackets

and 2nd query issue, searching in two fields using OR becomes

SELECT * FROM content WHERE MATCH(title) AGAINST('tom' IN BOOLEAN MODE) AND is_enabled = 1  AND (sub_category = 101 OR category = 200) ORDER BY seeders DESC LIMIT 0, 30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM