tags:

views:

22

answers:

3

Table 1

   slitem    firstname    lastname
    1         aaa           bbb

Table 2

slitem    firstname    lastname
 1         null           null
 2          null        null

Result :

slitem    firstname    lastname
    1         aaa           bbb
    2        null          null

i want to join this two table....

help me

A: 

It sounds like you are looking for a left outer join:

SELECT * FROM Table1 LEFT JOIN Table2 ON table2.slitem = Table1.slitem WHERE 1=1

Mike Cheel
where did you get left outer join from his criteria? He wants the non-null value for each slitem if it exists.
JNK
Yea you are right, I should have said right join.
Mike Cheel
+2  A: 
SELECT COALESCE(t1.slitem, t2.slitem) AS slitem, 
       COALESCE(t1.firstname, t2.firstname) AS firstname, 
       COALESCE(t1.lastname, t2.lastname) AS lastname
FROM      table1 t1 
FULL JOIN table2 t2 ON t1.slitem = t2.slitem

(Edit because OMG Ponies edited my FULL JOIN into a LEFT JOIN... which was not my intention!)

Lucero
A: 

You want a left join, just like the other answers said...but considering this is a rather basic thing to know when working SQL, you might want to read up a bit on it. Here's a good place to start.

Gio
Since `table1` has only 1 row, `table2` has 2 and the result has two, this would rather be a right join, or a full outer join as I posted.
Lucero
You're right, my mistake. I'm a noob too and missed that part, but the link is still good for a place for him to start reading :)
Gio