Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all

I have three tables accounts,location,division

accounts
=========
Id
desr
from_Location_Id
To_Location_Id
From_Division_Id
To_Division_Id

location
==========
locationId
location

Division
=========
DivisionId
Division

I want result data set like

Id Name from_locationname to_locationname to_divisionname from_divisionname
I inner joined to same table twice but entries coming only if from and to ids are same how to get names of from and to codes using query

Thanks in Advance
Amrutha
Posted

Use this


SQL
SELECT a.Id,a.desr,from_Location.Location,to_Location.Location,from_Division.Division,to_Division.Division
 FROM accounts a inner join location from_Location On a.from_Location_Id=from_Location.locationId
                 inner join location to_Location On a.To_Location_Id=to_Location.locationId
                 inner join Division from_Division On a.from_Division_Id=from_Division.DivisionId
                 inner join Division to_Division On a.To_Division_Id=to_Division.DivisionId



Hope this helps , If yes then accept it and vote it otherwise revert back with your queries.

--Rahul D.
 
Share this answer
 
Comments
amritha444 4-Feb-12 6:03am    
from_locationId and to_locationId are two fields in accounts table having foreign key reference with location table.this two fields in accounts table having ids from location master .same for division
RDBurmon 4-Feb-12 6:10am    
I got it . above query should work in that condition.
hi all
I got solution as

SQL
SELECT accounts.Id, accounts.Name, location_1.location as from_locationName, location.location AS To_location,
Division.Division As from_Division, Division_1.Division AS To_Division
FROM location AS location_1 RIGHT OUTER JOIN
Division RIGHT OUTER JOIN
accounts LEFT OUTER JOIN
Division AS Division_1 ON accounts.To_Division_Id = Division_1.DivisionId ON Division.DivisionId = accounts.From_Division_Id LEFT OUTER JOIN
location ON accounts.To_Location_Id = location.locationId ON location_1.locationId = accounts.from_Location_Id
 
Share this answer
 

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