2015-04-01 118 views
0

後,選擇唯一行有這個疑問:聯合查詢

(select status, cb_ebook, product_id, titel, auther, image from oc_product where status and cb_ebook and titel like '%term%') 
UNION    
(select status, cb_ebook, product_id, titel, auther, image from oc_product where status and cb_ebook and auther like '%term%') 

不過,也有在結果duplicatie行,我想只有唯一的product_id - 我試着按PRODUCT_ID但這不起作用。

任何解決方案?

回答

0

你可以嘗試在union查詢之後添加distinct,比如這個例子嗎?

SELECT distinct(x.status) status,x.cb_ebook,x.product_id,x.titel,x.auther,x.image 
FROM 
(
(select status, cb_ebook, product_id, titel, auther, image from oc_product where status and cb_ebook and titel like '%term%') 
UNION 
(select status, cb_ebook, product_id, titel, auther, image from oc_product where status and cb_ebook and auther like '%term%') 
) x 
+0

您好感謝所有的答案,我不能因爲我是新來的,還沒有mi,所以還沒有投票最低的聲譽! – Matthijs 2015-04-01 20:16:01

0

您不需要兩個查詢。只要把所有的where子句中

select status, cb_ebook, product_id, titel, auther, image 
from oc_product 
where status 
and cb_ebook 
and (auther like '%term%' or titel like '%term%') 
0

UES

UNION DISTINCT 

,而不是

UNION 

你可以試試這個

(select status, cb_ebook, product_id, titel, auther, image from oc_product where status and cb_ebook and titel like '%term%') 
UNION DISTINCT    
(select status, cb_ebook, product_id, titel, auther, image from oc_product where status and cb_ebook and auther like '%term%')