2011-09-08 102 views
2

我正在做一個選擇使用聯盟所有的搜索頁面,所以我需要知道從哪個表結果來自於,爲我做一個鏈接到正確的頁面。 有可能嗎?選擇使用聯盟所有疑問

例:

(SELECT id, title AS tit FROM video WHERE `title` LIKE '%test%') 
UNION ALL 
(SELECT id, title AS tit FROM testimonials WHERE `title` LIKE '%test%' AND _type = 'news') 
UNION ALL 
(SELECT id, title AS tit FROM image_gallery WHERE `title` LIKE '%test%' AND id_gallery = '1') 

乾杯芒果

回答

2

如何添加表名的SELECT語句?

(SELECT id, title AS tit, 'video' as tname FROM video WHERE `title` LIKE '%test%') 
UNION ALL 
(SELECT id, title AS tit, 'testimonials' as tname FROM testimonials WHERE `title` LIKE '%test%' AND _type = 'news') 
UNION ALL 
(SELECT id, title AS tit, 'image_gallery' as tname FROM image_gallery WHERE `title` LIKE '%test%' AND id_gallery = '1') 
+0

謝謝里克,我dindnt知道這是可能的! – Mango

4

只需將表名添加到每個子查詢:

(SELECT 'video' as tablename, id, title AS tit 
FROM video 
WHERE `title` LIKE '%test%') 
UNION ALL 
(SELECT 'testimonials' as tablename, id, title AS tit 
FROM testimonials 
WHERE `title` LIKE '%test%' AND _type = 'news') 
UNION ALL 
(SELECT 'image_gallery' as tablename, id, title AS tit 
FROM image_gallery 
WHERE `title` LIKE '%test%' AND id_gallery = '1')