Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
For eg:
I am having three tables as apple, ball and cat

apple contains only ID column(ID's of both ball and cat tables)

ball contains ID and Ball Names

cat contains ID and Cat Names.

Solution I Need:
how do I put join for these three tables to get a name of a single ID which I am passing?

My Query:

SQL
select b.ball_name, c.cat_name from tbl_ball b join tbl_apple a join tbl_cat c
on ((b.ballId = a.Id) or (c.catId = a.Id)) where a.Id = 1100
Posted
Comments
kumar2413 10-Jun-13 2:35am    
What is your table structure then. Put it so that it will help us to solve your solution
RelicV 10-Jun-13 3:12am    
Could you please provide the Schema and the data of the tables?

If your table structure is like this:
SQL
--Apple Table
ID      cat_id      ball_id
---------------------------
1100        1         1

--Ball Table
ball_id         ball_name
---------------------------
1                  Ball 1

--Cat Table
cat_id          cat_name
---------------------------
1                  Cat 1

Then your query should be like:
SQL
SELECT B.ball_name,C.cat_name FROM apple A
INNER JOIN ball B ON A.ball_id = B.ball_id
INNER JOIN cat C ON A.cat_id = C.cat_id
WHERE A.ID = 1100 -- ID should be identity column of apple table

Please refer:
MSDN : Join Fundamentals[^]
MSDN : Join Hints (Transact-SQL)[^]


--Amit
 
Share this answer
 
Comments
riodejenris14 10-Jun-13 4:37am    
---Apple table
ID
1100
1101
1102
1103
---Ball table
ID Name
1100 rubber
1102 stumper

---cat table
ID Name
1101 felix
1103 boose

this is my table structure.
_Amy 10-Jun-13 4:39am    
Then try this:
SELECT B.Name,C.Name FROM apple A
INNER JOIN ball B ON A.ID = B.ID
INNER JOIN cat C ON A.ID = C.ID
WHERE A.ID = 1100

But inner join will not return any rows. If you want all the rows from table apple then use Left Outer Join.
--Amit
riodejenris14 10-Jun-13 4:46am    
retrieves null value
_Amy 10-Jun-13 4:48am    
Since Inner Join will retrieve only matching value from all three tables, and you don't have any matching records, thats why it is returning empty recordset. Did you try Left Outer Join ? Try this:
SELECT B.Name,C.Name FROM apple A
LEFT OUTER JOIN ball B ON A.ID = B.ID
LEFT OUTER JOIN cat C ON A.ID = C.ID
WHERE A.ID = 1100

--Amit
riodejenris14 10-Jun-13 5:01am    
hurrah hey amy thankx yar its working!!!!!
Try
SQL
select b.ball_name, c.cat_name 
from tbl_ball b join tbl_apple a on b.ballId = a.Id
join tbl_cat c on c.catId = a.Id
where a.Id = 1100


[edit]Code block fixed[/edit]
 
Share this answer
 
v3
Comments
riodejenris14 10-Jun-13 2:31am    
it showing error abhinav 'incorrect syntax near keyword join'
Abhinav S 10-Jun-13 2:35am    
Perhaps this will help you - http://msdn.microsoft.com/en-us/library/aa213227%28v=sql.80%29.aspx.
Also note that you don't seem to have a common join between tables b and c.
SQL
SELECT b.ball_name,c.cat_name from tbl_apple a JOIN  tbl_ball b on a.id=b.id 
JOIN tbl_cat c on c.catid=a.id WHERE a.id=1100


Try this and see
 
Share this answer
 
Comments
riodejenris14 10-Jun-13 2:47am    
its not retrieving any value the data set is empty.
kumar2413 10-Jun-13 3:09am    
What is your table structure then. Put it so that it will help us to solve your solution

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