2

这个问题是关于 UNION 的索引或替代方案,它们的工作方式相同。

在进一步的例子中,我们假设title text并且contentfulltextindex

假设我们有三个语句

SELECT title as text FROM news ORDER BY score DESC
SELECT name as text FROM pages ORDER BY score DESC
SELECT content as text FROM comments ORDER BY score DESC

我们可以将它们组合起来(注意我们手动订购它们)

SELECT title as text FROM news ORDER BY score DESC
UNION
SELECT name as text FROM pages ORDER BY score DESC
UNION
SELECT content as text FROM comments ORDER BY score DESC

现在我们text可以打印了,但订单将是 1)新闻 2)页面 3)评论

这非常糟糕,因为我们希望那些可能按分数排序的不同

因此,我们可以将所有这些组合THREE SELECTSONE SELECT并排序

SELECT text FROM (
   SELECT title as text FROM news ORDER BY score DESC
   UNION
   SELECT name as text FROM pages ORDER BY score DESC
   UNION
   SELECT content as text FROM comments ORDER BY score DESC
) as combination

现在我们可以ORDER通过score

同样,这非常糟糕,因为分数是基于每个表而不是每个选择的

所以,我们继续前进,我的问题更接近了。

下一步是介绍MATCH() and AGAINST(),我们写

SELECT text, MATCH(text) AGAINST ('anything') as total_score FROM (
       SELECT title as text FROM news ORDER BY score DESC
       UNION
       SELECT name as text FROM pages ORDER BY score DESC
       UNION
       SELECT content as text FROM comments ORDER BY score DESC
    ) as combination ORDER BY total_score DESC

我们完了!text如果不是 MATCH( ) 似乎没有被索引为全文,那么一切都应该完美。

出现错误:Can't find FULLTEXT index matching the column list

现在一个 consufion 来了,可能除了以某种方式制作text一个FULLTEXT. 不幸的是,数据库中不存在这样的列text,假设您无法在 phpmyadmin 中找到它以使其成为FULLTEXT这种方式。

我想name as text进一步扩展以使其接近name as text type fulltext或类似的东西,我在任何地方都找不到。

请帮帮我,我想听听什么,也许在这种情况下还有另一种解决方法?我不知道。我将非常感谢。

4

1 回答 1

2

使用UNION ALL而不是UNION.

UNION 对联合结果集进行排序丢弃重复项(如果有)。

UNION ALL保留所选顺序(并保留重复项,如果有)

于 2012-12-16T19:17:40.883 回答