2015-04-01 103 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%')