在我的查询中,我遇到了一个问题,即FULLTEXT INDEX
无法归为DERIVED TABLE
,这似乎是正常的。
示例表明,data
无法编入FULLTEXT
的索引,但content
,text
和record
列已编入索引为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作为派生表并从中计算得分,但是这没有完成。如果引用没有INDEX,则会发生错误。 (就像上面的例子一样)。
这就是为什么我决定询问是否有可能通过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
类型的查询,并使其按预期运行。如果是这样,我会问你该怎么做。
答案 0 :(得分:1)
我不认为你可以这样做,因为这些表是无关的。但是,您应该能够通过忽略外部查询中的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