views:

60

answers:

2

Hello, I have two tables: A(col1,col2,col3,col4)

B(col1,col2,col3,col4)

Table A has 4 records(rows) and B has 6 rows.I want to join them like this,for example join them in C table

C(B.col1,B.col2,A.col3,A.col4,B.col3,B.col4) (tables have different values in records just col1 and col2 contains the same values)

when i join them on A.col1=B.col1 and A.col2=B.col2 I take Cartesian product :(

P.S I want to have 6 rows in C,where B.col1,B.col2,B.col3,B.col4 have 6rows and A.col3,A.col4 have 4 rows and other 2 null

please help me..

+1  A: 

Try

SELECT A.col1, A.col2, A.col3, A.col4, B.col3, B.col4
FROM A
FULL OUTER JOIN B on (A.col1 = B.col1 AND A.col2 = B.col2)
Manuel Faux
The need for an `OUTER JOIN` was added by an edit of the question shortly after my answer.
Manuel Faux
Thank you all of you.
kupa
@Manuel - I've taken the downvote off (-:
Murph
+2  A: 

You need to use a FULL OUTER JOIN

Select a.Col1,
       a.Col2,
       a.Col3,
       a.Col4,
       b.Col3,
       b.Col4

From TableA a
Full Outer Join TableB b on a.Col1 = b.Col1
                         And a.Col2 = b.Col2

EDIT:

"doesn't work" is not going to help anyone. Maybe you could provide further details as to why it isn't working for you.

You could add some example data and the actual expected output to your question.

e.g

Create Table #TableA
(
Col1 
...
)

Insert Into #TableA
Values (...)

The clearer your question then the better the answers will be. If people don't fully understand what your exact problem is and expected output you want then how are we supposed to provide you with a full and correct answer.

Barry
A reason why it does not work would be nice. What does this query you don't want it to do?
Manuel Faux
sorry,sorry.I was so hurry(because the problem had to be solved quickly) i missed important thing,that's why you weren't able to help me,it was my fault.For this question,what I have actually asked,your answer works...Thank you..But the problem was different and I have solved my problem:)
kupa
One never stops learning... "FULL" is a new one on me (and whilst I'm not even close to being an SQL Guru I'm not bad...)
Murph