加入两个Oracle查询

时间:2010-06-09 08:49:46

标签: oracle join

我要从两个表中查询并想要一个结果..我如何加入这两个查询?

第一个查询是从两个表查询,第二个查询是从一个表查询。

select pt.id,pt.promorow,pt.promocolumn,pt.type,pt.image,pt.style,pt.quota_allowed,ptc.text,pq.quota_left 

from promotables pt,promogroups pg ,promotablecontents ptc ,promoquotas pq where pt.id_promogroup = 1 and ptc.country ='049' and ptc.id_promotable = pt.id and pt.id_promogroup = pg.id and pq.id_promotable = pt.id order by pt.promorow,pt.promocolumn

select  pt.id,pt.promorow,pt.promocolumn,pt.type,pt.image,pt.style,pt.quota_allowed from promotables pt where pt.type='heading'

1 个答案:

答案 0 :(得分:4)

使用UNION或UNION ALL。只要您具有相同数量的列,并且它们是兼容的类型,应该执行您想要的任务。

SELECT pt.id, pt.promorow, pt.promocolumn, pt.type, pt.image, pt.style, pt.quota_allowed, ptc.text, pq.quota_left 
FROM promotables pt, promogroups pg, promotablecontents ptc, promoquotas pq 
WHERE pt.id_promogroup = 1
AND ptc.country ='049' 
AND ptc.id_promotable = pt.id
AND pt.id_promogroup = pg.id
AND pq.id_promotable = pt.id 
UNION
SELECT pt.id, pt.promorow, pt.promocolumn, pt.type, pt.image, pt.style, pt.quota_allowed, NULL, NULL
FROM promotables pt 
WHERE pt.type='heading'
ORDER BY 2, 3

如果要显示重复项(例如来自两个查询的相同行),请使用UNION ALL