views:

54

answers:

1

I've to query from two tables and want one result.. how can i join these two queries?

First query is querying from two tables and the second one is only from one.

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'
+2  A: 

Use UNION or UNION ALL. As long as you have the same number of columns and they are compatible types that should do what you want.

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

If you want to display duplicates (e.g identical rows coming from both queries), use UNION ALL

ChrisCM
for the first query the number result columns are more..
coder247
Just select NULL for the columns in the other query. This means you will still get the column and results from query 1 may have data there whereas rows from query 2 will always contain NULL for those columns.
ChrisCM