1

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?

jarlh
  • 42,561
  • 8
  • 45
  • 63
namja
  • 13
  • 3
  • 2
    See [this great explanation of joins](http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/). And don't use this old implicit join syntax any more. – juergen d May 27 '16 at 15:16
  • 3
    First thing to learn is explicit `JOIN` syntax instead of that old, comma separated, implicit join... – jarlh May 27 '16 at 15:16
  • @jarlh: I like how you think :) – juergen d May 27 '16 at 15:17
  • @juergend, when @namja knows the proper `INNER JOIN` syntax, it's so easy to take the next step to `LEFT JOIN`! – jarlh May 27 '16 at 15:18
  • @juergend Thank you! LEFT JOIN is definitely getting me in the right direction. – namja May 30 '16 at 22:53

1 Answers1

2

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

Community
  • 1
  • 1
BenBen
  • 65
  • 5