Question

So i'm just wondering how I would go about getting the a merged list of two tables, one with all the required rows, and the second with extra data to be associated with the first. enter image description here

So with table 1 and 2 I want to get 3, would this be able to be done as a query?

Was it helpful?

Solution

So if the "empty" value in column coming from table2 can just be NULL, you can do a LEFT JOIN

select 
t1.col1, --this is A, B, C
t1.col2, 
t1.col3, 
t2.col4 -- this is the fourth column
from Table1 t1
left join Table2 t2 on t1.Col1 = t2.Col1

see SqlFiddle

If you wanna be sure to join only if the 3 columns are the same, just add conditions to the left join

on t1.Col1 = t2.Col1 and t1.Col2 = t2.Col2 and t1.Col3 = t2.Col3

OTHER TIPS

Use Exists

SELECT T1.Column1, T1.Column2, T2.Column3
FROM T1, T2
WHERE T1.Column1 IN
  (SELECT Column1 FROM T1);

You could also try this for the sames results

SELECT T1.Column1, T1.Column2, T2.Column3
FROM T1 LEFT JOIN T2 ON T1.Column1 = T2.Column1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top