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 |
|
sqlmuppet
Starting Member
2 Posts |
Posted - 2009-11-24 : 06:56:51
|
| I have three tables: Person, LocalAddress, NonLocalAddressI want to select a list of Persons and their address. They will have EITHER a LocalAddress or NonLocalAddress, but not BOTH.Any clues on how I'd code this?Here's the two separate SELECTs I want to combine:SELECT Person.name, Person.idfrom Person(so for each Person above I want the following....)select addressline, idfrom LocalAddress JOIN Personwhere LocalAddress.id = Person.id union allselect addressline, idfrom NonLocalAddress, Personwhere NonLocalAddress.id = Person.idThanks in advance! |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-24 : 07:29:12
|
[code]select Person.name, Person.id,isnull(local.addressline,nonlocal.addressline) as addresslinefrom Personleft join LocalAddress as local on local.id = Person.idleft join NonLocalAddress as nonlocal on nonlocal.id = Person.id[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
sqlmuppet
Starting Member
2 Posts |
Posted - 2009-11-24 : 08:18:58
|
Thanks webfred - that worked a treat! |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-24 : 08:27:03
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|