| | |
LEFT JOIN problem
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
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:
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
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:
sql Syntax (Toggle Plain Text)
SELECT t1.id as id1, t1.name1 as name1, t2.name2 as name2, t3.name3 as name3 FROM (test1 t1) LEFT JOIN (test3 t3, test2 t2) ON (t1.id=t2.id AND t1.id=t3.id)
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
•
•
Join Date: Feb 2008
Posts: 42
Reputation:
Solved Threads: 13
0
#2 21 Days Ago
MS SQL Syntax (Toggle Plain Text)
CREATE TABLE #test1 (ID int, name1 varchar(255)) INSERT #test1 values (1, 'value1') CREATE TABLE #test2 (ID int, name2 varchar(255)) INSERT #test2 values (1, 'value2') CREATE TABLE #test3 (ID int, name3 varchar(255)) INSERT #test3 values (3, 'value3_01') INSERT #test3 values (4, 'value3_02') SELECT #test1.ID, name1, name2, name3 FROM #test1 inner join #test2 on #test1.ID = #test2.ID left join #test3 on #test1.ID = #test3.ID DROP TABLE #test1 DROP TABLE #test2 DROP TABLE #test3
Hence Wijaya
www.ex-Soft.tk
www.ex-Soft.tk
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#3 21 Days Ago
•
•
•
•
MS SQL Syntax (Toggle Plain Text)
CREATE TABLE #test1 (ID int, name1 varchar(255)) INSERT #test1 values (1, 'value1') CREATE TABLE #test2 (ID int, name2 varchar(255)) INSERT #test2 values (1, 'value2') CREATE TABLE #test3 (ID int, name3 varchar(255)) INSERT #test3 values (3, 'value3_01') INSERT #test3 values (4, 'value3_02') SELECT #test1.ID, name1, name2, name3 FROM #test1 inner join #test2 on #test1.ID = #test2.ID left join #test3 on #test1.ID = #test3.ID DROP TABLE #test1 DROP TABLE #test2 DROP TABLE #test3
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.
•
•
Join Date: Feb 2008
Posts: 42
Reputation:
Solved Threads: 13
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:
sql Syntax (Toggle Plain Text)
SELECT #test1.ID, case when #test1.id <> isNull(#test2.id,0) then null else name1 end as name1, name2, name3 FROM #test1 left join #test2 on #test1.ID = #test2.ID left join #test3 on #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
www.ex-Soft.tk
•
•
Join Date: Oct 2008
Posts: 10
Reputation:
Solved Threads: 4
0
#5 21 Days Ago
Well, I think
might work correctly...
MS SQL Syntax (Toggle Plain Text)
SELECT t1.id AS id1, t1.name1, t2.name2, t3.name3 FROM test1 t1 LEFT OUTER JOIN test2 t2 ON t1.id = t2.id LEFT OUTER JOIN test3 t3 ON t1.id = t3.id
![]() |
Similar Threads
- Help!howto put the where clauses in left join (PHP)
- Problem with join? (MySQL)
- Can someone help me my LEFT JOIN? (PHP)
- Help on DataList and Repeater Problem (ASP.NET)
- left join (Visual Basic 4 / 5 / 6)
- Firefox left-marin problem (HTML and CSS)
- Same problem (PHP)
Other Threads in the MS SQL Forum
- Previous Thread: Get records from current year
- Next Thread: 'UPDATE'ing table with same value over and over again!
| Thread Tools | Search this Thread |





