0

在我的查询中,我遇到了FULLTEXT INDEX无法分配给 a的问题DERIVED TABLE,这似乎是正常的。

示例演示了data不能被索引为FULLTEXT,但是contenttextrecord列已经被索引,FULLTEXT因为它们在“真实”表中

SELECT `data` FROM SELECT(
   SELECT `content` as `data` FROM `table1`
   UNION SELECT `text` as `data` FROM `table2`
   UNION SELECT `record` as `data` FROM `table3`
) as `search`

在我的实际代码中,我使用SELECT MATCH() AGAINST()表中的分数并将它们联合在一起以获得score每个表的分数,这些 UNIONS 是子查询和第一级(主)SELECT 然后将这些 UNIONS 选择为派生表并从中计算分数,但是这是尚未完成。发生错误,指出引用没有索引。(就像上面的例子一样)。

这就是为什么我决定询问是否可以通过 JOINS 实现我的目标的原因。

这是我的实际(工作)代码:

SELECT *,MATCH(`data`) AGAINST('keyword' IN BOOLEAN MODE) as `relevance` 
FROM (                  
        SELECT CONCAT(`title`,' ',`content`) as `data`,`id`,'tmp_pages' as `table`, 
        MATCH(`title`,`content`) AGAINST ('keyword') AS `score` 
        FROM `tmp_pages` 
        WHERE MATCH(`title`,`content`) AGAINST ('keyword')
        UNION 
        SELECT CONCAT(`title`,' ',`content`) as `data`, `id`,'tmp_news' as `table`, 
        MATCH(`title`,`content`) 
        AGAINST ('keyword') AS `score` 
        FROM `tmp_news` 
        WHERE MATCH(`title`,`content`) AGAINST ('keyword')
        UNION 
        SELECT CONCAT(`title`,' ',`content`) as `data`,`id`,'tmp_comments' as `table`, 
        MATCH(`title`, `content`) 
        AGAINST ('keyword') AS `score` FROM `tmp_comments` 
        WHERE MATCH(`title`, `content`) AGAINST('keyword')
        UNION 
        SELECT CONCAT(`manufacturer`,' ',`model`,' ',`location`,' ',`other`,' ',`contact`) as `data`,`id`,'tmp_auction_auto' as `table`,
        MATCH(`manufacturer`,`model`,`location`,`other`,`contact`) AGAINST ('keyword') AS `score` 
        FROM `tmp_auction_auto` 
        WHERE MATCH(`manufacturer`,`model`,`location`,`other`,`contact`) 
        AGAINST ('keyword')
) as `search` ORDER BY `relevance` DESC

我想知道是否可以将这部分转换JOIN为查询类型,并使其按预期运行。如果是这样,我会问如何做到这一点。

4

1 回答 1

1

我认为您不能将其作为 JOIN 来执行,因为这些表是不相关的。但是,您应该能够通过简单地relevance从外部查询中省略列并按以下顺序排序来消除错误score

SELECT *
FROM (                  
        SELECT CONCAT(`title`,' ',`content`) as `data`,`id`,'tmp_pages' as `table`, 
        MATCH(`title`,`content`) AGAINST ('keyword') AS `score` 
        FROM `tmp_pages` 
        WHERE MATCH(`title`,`content`) AGAINST ('keyword')
        UNION 
        SELECT CONCAT(`title`,' ',`content`) as `data`, `id`,'tmp_news' as `table`, 
        MATCH(`title`,`content`) 
        AGAINST ('keyword') AS `score` 
        FROM `tmp_news` 
        WHERE MATCH(`title`,`content`) AGAINST ('keyword')
        UNION 
        SELECT CONCAT(`title`,' ',`content`) as `data`,`id`,'tmp_comments' as `table`, 
        MATCH(`title`, `content`) 
        AGAINST ('keyword') AS `score` FROM `tmp_comments` 
        WHERE MATCH(`title`, `content`) AGAINST('keyword')
        UNION 
        SELECT CONCAT(`manufacturer`,' ',`model`,' ',`location`,' ',`other`,' ',`contact`) as `data`,`id`,'tmp_auction_auto' as `table`,
        MATCH(`manufacturer`,`model`,`location`,`other`,`contact`) AGAINST ('keyword') AS `score` 
        FROM `tmp_auction_auto` 
        WHERE MATCH(`manufacturer`,`model`,`location`,`other`,`contact`) 
        AGAINST ('keyword')
) as `search` ORDER BY `score` DESC
于 2012-12-17T19:03:04.253 回答