LEFT JOIN problem

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 2
Reputation: pc131 is an unknown quantity at this point 
Solved Threads: 0
pc131 pc131 is offline Offline
Newbie Poster

LEFT JOIN problem

 
0
  #1
21 Days Ago
Hi All!

I have 3 tables:

test1
id name1
1 value1

test2
id name2
1 value2

test3
id name3
3 value3_01
4 value3_02

I want to display all records of table test1 with values of table test2 (test1.id = test2.id) and values of test3 (if test.id match with test1.id) - in this case test3 has no common ids with test1 so NULLs are displayed.
How to make sql query to display:
id1 name1 name2 name3
1 value1 value2 NULL

I constructed query:
  1. SELECT t1.id as id1, t1.name1 as name1, t2.name2 as name2, t3.name3 as name3
  2. FROM (test1 t1) LEFT JOIN (test3 t3, test2 t2)
  3. ON (t1.id=t2.id AND t1.id=t3.id)
but it gives me:

id1 name1 name2 name3
1 value1 NULL NULL

name2 is NULL instead of desired "value2". WHY?

LEFT JOIN DEFINITION:
SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all rows from the left table, even if there are no matches in the right table.

There are no matches in test3 table when joining test1 table so name3 from table3 is NULL in result.

BUT There are matches in table test2 (test2.id=1 and test1.id=1) so why null is displayed?

Hope anyone can help me. I am in big trouble.

Thanks in advace.
Tom
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster
 
0
  #2
21 Days Ago
  1. CREATE TABLE #test1 (ID int, name1 varchar(255))
  2. INSERT #test1 values (1, 'value1')
  3.  
  4.  
  5. CREATE TABLE #test2 (ID int, name2 varchar(255))
  6. INSERT #test2 values (1, 'value2')
  7.  
  8. CREATE TABLE #test3 (ID int, name3 varchar(255))
  9. INSERT #test3 values (3, 'value3_01')
  10. INSERT #test3 values (4, 'value3_02')
  11.  
  12. SELECT #test1.ID, name1, name2, name3
  13. FROM #test1 inner join #test2 on
  14. #test1.ID = #test2.ID left join #test3 on
  15. #test1.ID = #test3.ID
  16.  
  17. DROP TABLE #test1
  18. DROP TABLE #test2
  19. DROP TABLE #test3
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: pc131 is an unknown quantity at this point 
Solved Threads: 0
pc131 pc131 is offline Offline
Newbie Poster
 
0
  #3
21 Days Ago
Originally Posted by huangzhi View Post
  1. CREATE TABLE #test1 (ID int, name1 varchar(255))
  2. INSERT #test1 values (1, 'value1')
  3.  
  4.  
  5. CREATE TABLE #test2 (ID int, name2 varchar(255))
  6. INSERT #test2 values (1, 'value2')
  7.  
  8. CREATE TABLE #test3 (ID int, name3 varchar(255))
  9. INSERT #test3 values (3, 'value3_01')
  10. INSERT #test3 values (4, 'value3_02')
  11.  
  12. SELECT #test1.ID, name1, name2, name3
  13. FROM #test1 inner join #test2 on
  14. #test1.ID = #test2.ID left join #test3 on
  15. #test1.ID = #test3.ID
  16.  
  17. DROP TABLE #test1
  18. DROP TABLE #test2
  19. DROP TABLE #test3
Hi, thanks for your input.

But when test2.id=2 the result of this query is empty. And I want then the result to be:

id1 name1 name2 name3
1 NULL NULL NULL.

I think I tried INNER JOIN test1 and test2 table, but I can have situations when test2.id doesn't match test1.id. Still I want to display ALL records of test1, but NULLS should be in test2 and test3 columns if no appropriate ids were found (test1.id=1, test2.id=2, test3.ids=3,4.....).

Why LEFT JOIN doesn't display results for table test 2 in which ids/id match id from "left" table test 1, if I join anther one table test3 in which id don't match ids from left table? Then result is: data from "left table" - this is OK, data from test3 table as NULL - this is OK, but data from test2 table should be value2, because test1.id=test2 id?????

Thanks in advance for your help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster
 
0
  #4
21 Days Ago
I do not understand why if test2.id = 2 then the result will 1 NULL NULL NULL. if you want that result try below:

  1. SELECT #test1.ID, case when #test1.id <> isNull(#test2.id,0) then null else name1 end as name1, name2, name3
  2. FROM #test1 left join #test2 on
  3. #test1.ID = #test2.ID left join #test3 on
  4. #test1.ID = #test3.ID
Last edited by peter_budo; 20 Days Ago at 5:35 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 10
Reputation: Celt.Max is an unknown quantity at this point 
Solved Threads: 4
Celt.Max Celt.Max is offline Offline
Newbie Poster
 
0
  #5
21 Days Ago
Well, I think
  1. SELECT t1.id AS id1, t1.name1, t2.name2, t3.name3
  2. FROM test1 t1
  3. LEFT OUTER JOIN test2 t2 ON t1.id = t2.id
  4. LEFT OUTER JOIN test3 t3 ON t1.id = t3.id
might work correctly...
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC