Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
In my sql query, I have to join more than 6 tables to select a record. In this some of the tables contains null values. Because of this null values i couldn't retrieve any records.

For ex,

My query looks like this

SQL
Select columns from table1 join table2 on tabl1.c1 = table 2.c join table3 on table1.c2 = table3.c join table4 on table1.c3 = table4.c join table5 on table1.c4=table5.c  ........... where id=value
like this.


Here
----> In this line join table4 on table1.c3 = table4.c

table1.c3 is null... So I am unable to select records.

My question is If that column is null then just return null values with that records.

How can i solve it?
Posted
Updated 12-Aug-12 19:40pm
v2
Comments
__TR__ 13-Aug-12 1:57am    
You can use ISNULL() function for assigning a default value for records containing NULL.
Eg: join table4 on ISNULL(table1.c3,'') = ISNULL(table4.c,'')

Use Left outer join for table4 on table1.c3 = table4.c, then all the records from table table1 will be retrieved.

Thanks
Ashish
 
Share this answer
 
Comments
Velkumar Kannan 13-Aug-12 1:46am    
no need to select all records. just select a record which have this ID value.
AshishChaudha 13-Aug-12 1:57am    
Then your query is giving right result.
You need to use an outer join in this kind of situation. Outer join returns rows from a table even if it has no corresponding rows in the joined table.

The outer join can be a left outer join or a right outer join depending on the order you list the tables.

Have a look at all of the join examples in SQL Joins[^], especially SQL LEFT JOIN[^] and SQL RIGHT JOIN[^]
 
Share this answer
 
Comments
Velkumar Kannan 13-Aug-12 1:56am    
But one of the column contains NULL values. So there is a problem while comparing

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