联合查询
问题描述:
后,选择唯一行有这个疑问:联合查询
(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但这不起作用。
任何解决方案?
答
你可以尝试在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
答
您不需要两个查询。只要把所有的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%')
答
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%')
您好感谢所有的答案,我不能因为我是新来的,还没有mi,所以还没有投票最低的声誉! – Matthijs 2015-04-01 20:16:01