inner join same table twice

Hi
I am trying to get "state name" and "desired state name" based on stateid which is in another table. For this i have to do join twice on state table. But when i do that, i don't get any results. I have all values.

can anyone give a soultion for this problem.
Query:
Select u.displayname, up.*, a.avatarpic, tc.countryname, ts.statename, isnull(tds.statename,0) as desiredstatename, ut.usertype
from             tbl_User u
inner join       tbl_userprofile up on u.userid=up.userid
inner join      tblCountries tc on tc.countryid = up.country
inner join      tblStates ts on ts.stateid = up.state
inner join      tblStates tds on tds.stateid = up.desiredstate
inner join      tbl_userType ut on ut.usertypeID = u.usertypeID
inner join       tbl_avatar a on u.avatarid=a.avatarid
Where      u.Userid = @Userid

Thanks in advance
LVL 1
coolcyberAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wstuphCommented:
Do you get results if you change it to a left outer join?  If you're inner joining and it's not there then it's not going to show up.
0
rafranciscoCommented:
If the desired state is null then you won't get any record for the user.  Instead of using INNER JOIN, try using a LEFT JOIN:

Select u.displayname, up.*, a.avatarpic, tc.countryname, ts.statename, isnull(tds.statename,0) as desiredstatename, ut.usertype
from          (tbl_User u
inner join      tbl_userprofile up on u.userid=up.userid
inner join     tblCountries tc on tc.countryid = up.country
inner join     tblStates ts on ts.stateid = up.state
inner join     tbl_userType ut on ut.usertypeID = u.usertypeID
inner join      tbl_avatar a on u.avatarid=a.avatarid)
left join tblStates tds on tds.stateid = up.desiredstate
Where     u.Userid = @Userid
0
SireesCommented:
Can you try this???

Select u.displayname, up.*, a.avatarpic, tc.countryname, ts.statename, isnull(tds.statename,0) as desiredstatename, ut.usertype
from           tbl_User u
inner join      tbl_userprofile up on u.userid=up.userid
inner join     tblCountries tc on tc.countryid = up.country
left join     tblStates ts on ts.stateid = up.state
left join     tblStates tds on tds.stateid = up.desiredstate
inner join     tbl_userType ut on ut.usertypeID = u.usertypeID
inner join      tbl_avatar a on u.avatarid=a.avatarid
Where     u.Userid = @Userid
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

coolcyberAuthor Commented:
when i use "left join" i get a result  but with "NULL" in desiredstate column.
But i have a value in desiredState column
0
coolcyberAuthor Commented:
Sirees:
tried that, but no result in desiredstate column
0
coolcyberAuthor Commented:
my bad i was looking into wrong column
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.