How to: match (search space) against (join with column from other table)

℡╲_俬逩灬. 提交于 2019-12-11 05:58:26

问题


I'm lousy at SQL queries so this may be a silly question. However, here is roughly what i'd like to do:

table corpuses //stuff i'd like to search thru title body ...

table searches //list of search terms to be applied to corpuses term
...

The query i'd like to write is more or less as follows: I beleive I need some sort of a join, but I'm not sure just how to do that. Additionally, I'm not sure that the against() operator will take anything aside from a literal - the docs didn't seem to mention either way.

select * from corpuses where match (title, body) against (select term from searches);

I'm using MySQL 5

Any thoughts are greatly appreciated.

Thanks! Brian


回答1:


Sounds like you need to use a FULLTEXT matching expression in your join condition.

I've never used a fulltext match in a join condition, so I'm not sure this will work, but hypothetically this might do it:

SELECT DISTINCT c.*
FROM corpuses c JOIN searches s 
  ON (MATCH(c.title, c.body) AGAINST (s.term));

Okay I've tried it using your table definitions and some sample data from the MySQL manual. Here's a query that works (tested with MySQL 5.1.30):

SELECT *
FROM corpuses 
WHERE MATCH(title, body)
  AGAINST ( (SELECT GROUP_CONCAT(term SEPARATOR ' ') FROM searches) 
    IN BOOLEAN MODE);


来源:https://stackoverflow.com/questions/496799/how-to-match-search-space-against-join-with-column-from-other-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!