Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have two table Like table A and Table B As

  Table A                   Table B   
                      
PiD     Val               FID    VAL2
 1       A                 2      E
 2       B                 3      M 
 3       C                 6      L
 4       D                 7      P
 5       E


I want a table with he help of SQL statement Like

Table C

Pid   Val        Val2
 1     A          Null
 2     B          E
 3     C          M
 4     D          Null
 5     E          Null
 6     Null       L
 7     Null       P

Please reply soon...
Posted
Updated 27-Dec-13 10:09am
v3
Comments
CHill60 27-Dec-13 14:46pm    
What have you tried?
ZurdoDev 27-Dec-13 14:50pm    
I don't follow. What is the output you are expecting?

SQL
SELECT COALESCE(Pid, FID) as Pid, Val, Val2
FROM A
FULL JOIN B ON A.Pid=B.FID


This will give you the expected result, as you need both rows from two tables then you need to use Full Join instead of Left (or Right) Outer Joins.

For further reference please go through
http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/[^]
 
Share this answer
 
v2
Comments
Christian Graus 27-Dec-13 15:23pm    
You're right, I missed that, and I bet that's why he was confused, he probably was trying left joins.
CHill60 27-Dec-13 16:10pm    
Yep I missed those last two lines in the results too. 5'd. Next time I'll format the question first ;-p
SQL
SELECT Pid, Val, Val2
FROM A
LEFT OUTER JOIN B ON A.Pid=B.FID

This is very simple stuff so you might find this link useful too
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html[^]
 
Share this answer
 
Just to add, an inner join means 'only return values that match both tables'. A left outer join means 'return all the rows to the left of me, and any that don't have values in the table on the right, return null'. A full join returns all values from both tables, returning nulls for missing rows.

http://www.codeproject.com/Articles/700317/SQL-Wizardry-Episode-One-Joins[^] is my article on SQL joins.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900