2014-04-23 62 views
0
SELECT id, title, keywords, post, img, 
     NULL as device_id, null as mfgr, null as model, null as img 
     FROM blog_posts WHERE MATCH(title, desc_meta, keywords_meta, post) AGAINST('boost') 
     UNION ALL 
     SELECT NULL, NULL, NULL, NULL, id AS device_id, mfgr, model, img, summary 
     FROM device_specs WHERE MATCH(mfgr, model, summary) AGAINST('boost'); 

考慮上面的查詢,兩個表格沒有任何共同之處。這僅用於搜索腳本。我將如何去保持與兩個組合表的相關性?上面的查詢工作,但在所有情況下,blog_posts的結果出現在device_specs的結果之前。mysql:聯合所有和按相關性排序?

+0

將其包裝在子查詢中,並且只是按'order by'? – Peter

回答

2

您想要將相關性添加到每個子查詢中。您可以在沒有子查詢的情況下執行此操作:

SELECT id, title, keywords, post, img, 
     NULL as device_id, null as mfgr, null as model, null as img, 
     MATCH(title, desc_meta, keywords_meta, post) AGAINST('boost') as relevance 
FROM blog_posts 
WHERE MATCH(title, desc_meta, keywords_meta, post) AGAINST('boost') > 0 
UNION ALL 
SELECT NULL, NULL, NULL, NULL, id AS device_id, mfgr, model, img, summary, 
     MATCH(mfgr, model, summary) AGAINST('boost') as relevance 
FROM device_specs 
WHERE MATCH(mfgr, model, summary) AGAINST('boost') > 0 
ORDER BY relevance; 
+0

感謝一幫朋友,救了我小時,相信我! – Keezy