James B
James B

Reputation: 159

SQL Join from 2 Tables with Null Values

I have 2 tables and want to pull the results back from them into one. Now the Name field is a unique ID with multiple data attached to it, i.e. the dates and the times. I've simplified the data somewhat to post here but this is the general gist.

Table 1

Name    Date
John    12th
John    13th
John    15th
John    17th

Table 2

Name    Colour
John    Red
John    Blue
John    Orange
John    Green

Result Needed

Name    Date    Time
John    12th    NULL
John    13th    NULL
John    15th    NULL
John    17th    NULL
John    NULL    Red
John    NULL    Blue
John    NULL    Orange
John    NULL    Green

I'm currently performing a Left join to pull the data however it is posting the results next to each other like

John 12th Red

Upvotes: 0

Views: 56

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271161

You want union all:

select name, date, null as colour
from t1
union all
select name, null, colour
from t2;

I took the liberty of naming the second column colour rather than time, simply because that makes more sense in the context of the question.

Upvotes: 4

Related Questions