namja
namja

Reputation: 13

Return NULL if no rows are found SQL

newbie to SQL, please help. I only know basic syntax but know I can't accomplish what I want to with it:

Have two tables:

user_table:

id   name
1   george
2   harry
3   ralph

updown_table:

id  updown
1   up
3   down

My query:

select  
    u.id,
    u.name,
    up.updown
from    
    user_table u, updown_table up
where   
    u.id = up.id;

I'd like it to return id's 1, 2, 3 and put a NULL value in for 2. But obviously as the entry doesn't exist in updown, it will only return 1 and 3.

Any help, please?

Upvotes: 1

Views: 112

Answers (1)

BenBen
BenBen

Reputation: 65

Maybe try this?

Select  
    u.id,
    u.name,
    up.updown
From    
    user_table u left join updown_table up ON u.id=up.id;

Also as a reference for you: Difference between JOIN and INNER JOIN

Upvotes: 2

Related Questions