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:

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)

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

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
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

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.

I do not understand why if test2.id = 2 then the result will 1 NULL NULL NULL. if you want that result try below:

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

Well, I think

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

might work correctly...

outer join

Sol : SQL > Select last_name, d.department_id, d.name from employee e, department d where e.department_id(+)=d.department_id and d.department_idin (select department_id from department where name IN (‘SALES’,’OPERATIONS’))

50+ basic and advanced queires
Click Here

Hi All!
I have idea regarding this only left join between two table.
If u have idea 3 to 4 table .
I hardly interested to know.

regards
prem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.