2011-05-30 3 views
1

我公司通過與全​​文檢索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已設置我檢測,如果沒有,如果議程標題已定等 但結果總是從第一個查詢(頁)。因此,如果訪問者搜索並查找新聞項目,則會在此情況下顯示爲頁面。

所以我的問題是;我究竟做錯了什麼?這是做這件事的最好方法嗎?

在此先感謝!

回答

1

當您使用UNIONs那樣,添加一個決定因素列,只是一個常數,對每個表,讓你知道每個條目來自哪個表的。

SELECT source, id, title, score 
    FROM 
    (SELECT 'pages' AS source, id, title, 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, id, title, 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, id, 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 

您還需要在SELECT在整個組合集來包裝它ORDER BY,你有那隻在最後一個表,我認爲順序的方式。改變title屬性不是一個好主意,或者,作爲列名必須是相同的UNION或將與第一個遇到的標題,或只是空白列名合併。

1

嘗試像查詢:

SELECT 'pages' AS used_table,id, title, 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,id, title , 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,id, 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 

,並檢查used_table現場檢查使用巫表。