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.
| 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 tableselect country.arrive, country.depart, state.code as arrivecode, state.code as departcodefrom countryinner join stateon country.arrivestation=state.stationor country.departstation=state.stationBut 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 departcodefrom countryinner join state depon country.arrivestation=dep.stationinner join state arron country.departstation=arr.station- LumbagoMy blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/ |
 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-03-26 : 02:35:51
|
| May I know why the OR won't works? |
 |
|
|
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. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|