Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Inner join

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-03-26 : 02:10:23
I would like to have the output of countrycode depends on the country.

I have a country table and state table

select country.arrive, country.depart, state.code as arrivecode, state.code as departcode
from country
inner join state
on country.arrivestation=state.station
or country.departstation=state.station

But it populate out the arrive code as same as depart code.
Is my inner join wrong?

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2012-03-26 : 02:18:46
Yes, you need to join twice instead:

select country.arrive, country.depart, arr.code as arrivecode, dep.code as departcode
from country
inner join state dep
on country.arrivestation=dep.station
inner join state arr
on country.departstation=arr.station

- Lumbago
My blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-03-26 : 02:35:51
May I know why the OR won't works?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-26 : 05:22:07
quote:
Originally posted by peace

May I know why the OR won't works?


Write down your own sample data and see what happens (i.e. what the joins will do) then you will know why OR can't work.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-26 : 09:17:20
YOu are evaluating TRUE or FALSE, for that particular code value. If you have a second Value, then you need to go back to the table and find THAT Row that matches the second value

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -