在全文搜索中检索受影响的表

问题描述

| 我正在使用全文搜索搜索3个不同的表,我需要检测结果来自哪个表。这就是我现在所拥有的:
SELECT id,title AS page_title,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM pages WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) UNION
SELECT id,title AS agenda_title,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM agenda WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) UNION
SELECT id,title AS news_title,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM news WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) ORDER BY score ASC
我正在检测是否已设置page_title,是否已设置议程标题等。 但是结果总是来自第一个查询页面)。因此,如果访问者搜索并找到新闻,在这种情况下它将显示页面。 所以我的问题是;我究竟做错了什么?这是最好的方法吗? 提前致谢!     

解决方法

        当您像这样使用
UNIONs
时,请向每个表中添加一个行列式列(仅一个常量),以使您知道每个条目来自哪个表。
SELECT source,id,title,score
    FROM
    (SELECT \'pages\' AS source,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score
        FROM pages WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE)
    UNION
    SELECT \'agenda\' AS source,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score 
        FROM agenda WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) UNION
    SELECT \'news\' AS source,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score 
        FROM news WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE)
) ORDER BY score ASC
您还需要在整个组合集合中将它包装在
SELECT
ORDER BY
中,就像您只在我认为的最后一张桌子上订购一样。更改
title
属性也不是一个好主意,因为
UNION
的列名必须相同,否则它将与第一个遇到的标题合并,或者仅将列名空白。     ,        尝试如下查询:
SELECT \'pages\' AS used_table,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM pages WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) UNION
SELECT \'agenda\' AS used_table,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM agenda WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) UNION
SELECT \'news\' AS used_table,MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM news WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) ORDER BY score ASC
并检查used_table字段以检查是否使用了女巫表。