简体   繁体   English

MySQL可选LEFT JOIN与MATCH

[英]MySQL Optional LEFT JOIN With MATCH

I have the following query, which performs a full text search against two columns in two different tables for the same search term in a MySQL Innodb database; 我有以下查询,它对MySQL Innodb数据库中相同搜索项的两个不同表中的两列执行全文搜索;

SELECT Id, 
MATCH (tb1.comment, tb2.comment) AGAINST (+'search term' IN BOOLEAN MODE) AS Relevance
FROM tbl1
LEFT JOIN tb2 ON tb1.Id = tb2.Id
WHERE MATCH (tb1.comment, tb2.comment) AGAINST (+'search term' IN BOOLEAN MODE) 
HAVING Relevance > 0 

If I perform the MATCH on just tb1.comment it works fine and I get back the relevant search terms, but I want to perform it against both columns. 如果我只在tb1.com上执行MATCH它工作正常,我会找回相关的搜索条件,但我想对两个列执行它。

However because the other table is optional with the LEFT JOIN it doesn't return anything, if there is no matching Ids. 但是因为另一个表是LEFT JOIN的可选项,如果没有匹配的ID,它不会返回任何内容。 Any ideas on how to overcome this problem ? 关于如何克服这个问题的任何想法?

I managed to figure out the following work around that appears to perform fine and give the results I desire; 我设法弄清楚以下的工作似乎表现良好,并给出我想要的结果;

    SELECT Id, 
    MATCH (tb1.comment) AGAINST (+'search term' IN BOOLEAN MODE) AS Relevance1,
    MATCH (tb2.comment) AGAINST (+'search term' IN BOOLEAN MODE) AS Relevance2
    FROM tbl1
    LEFT JOIN tb2 ON tb1.Id = tb2.Id
    WHERE (MATCH (tb1.comment) AGAINST (+'search term' IN BOOLEAN MODE) 
    OR MATCH ( tb2.comment) AGAINST (+'search term' IN BOOLEAN MODE))
    HAVING (Relevance1+Relevance2) > 0 
    ORDER BY (Relevance1+Relevance2) DESC

You can't match against multiple columns that aren't in the same FULLTEXT index. 您无法匹配不在同一FULLTEXT索引中的多个列。 From http://dev.mysql.com/doc/refman/5.6/en/fulltext-restrictions.html : 来自http://dev.mysql.com/doc/refman/5.6/en/fulltext-restrictions.html

The MATCH() column list must match exactly the column list in some FULLTEXT index definition for the table, unless this MATCH() is IN BOOLEAN MODE on a MyISAM table. MATCH()列列表必须与表的某些FULLTEXT索引定义中的列列表完全匹配,除非此MATCH()在MyISAM表上是IN BOOLEAN MODE。 For MyISAM tables, boolean-mode searches can be done on nonindexed columns, although they are likely to be slow. 对于MyISAM表,布尔模式搜索可以在非索引列上完成,尽管它们可能很慢。

In your particular case, you don't have an index consisting of exactly (tb1.comment, tb2.comment) —nor can you—so the match can't ever succeed. 在你的特定情况下,你没有一个完全由(tb1.comment, tb2.comment)组成的索引 - 你可以 - 所以这场比赛不可能成功。

To get this to work, create a third table linked to those tables containing the two comment fields, with both columns indexed, and perform your match against that in an apppropriate JOIN . 要使其工作,请创建一个链接到包含两个comment字段的表的第三个表,并将两个列编入索引,并在适当的JOIN执行匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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