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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2003-12-02 : 08:27:39
|
| Zackie writes "Hello.I'm trying to join a table to another table in order to get a value from the second table. This needs to be done twice in the same query.The database tables are as follows:table SUBsub_idsub_titlesub_descsub_p_one_idsub_p_two_idtable Pp_idp_nameExample:SUBsub_id | sub_title | sub_desc | sub_p_one_id | sub_p_two_id1 | none | none | 1 | 22 | n0n3 | n0ne | 3 | 3Pp_id | p_name1 | Zack2 | Jay Yaj3 | No Name4 | Don't really careNow I need an SQL query that returns a result set for a specified (sub_id). Exampleif submited id = 1 it returns1 | none | none | 1 | 2 | Zack | Jay Yaiif submited id = 2 it returns2 | n0n3 | n0ne | 3 | 3 | No Name | No NameIf I had only one field to connect, I would use a simple inner join "connecting" the two tables "on sub_p_one_id = p_id".So my question is: how can a do a similar query that joins a table to a second table twice, getting a single result set, as in the examples above.Thanks in advance." |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2003-12-02 : 09:49:29
|
| [code]select a.sub_id,a.sub_title,a.sub_desc,a.sub_p_one_id, a.sub_p_two_id, b.p_name, c.p_namefrom sub aleft join p b on b.p_id = a.sub_p_one_id left join p c on c.p_id = a.sub_p_two_id where a.sub_id = 2[/code] |
 |
|
|
|
|
|