Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing several foreign keys with their values

Status
Not open for further replies.

Malaphus

Programmer
Nov 4, 2004
4
US
I have 2 tables:
Table 1:
5 fields: field1_id, field2_link1, field3_link2, field4_link3, field5_link4

Table 2:
2 fields: field1_id, field2_name

fields 2 thru 5 in table 1 contain integer values that point to the cooresponding field1_id's in table 2. I simply want a select statement that will return all of table1 but instead of returning the actual values for fields 2 thru 5 i want it to return the field2_name from table2 in place.... If that makes any sense

Example:
Table 1:
1 204 347 470 360 187
2 347 470 204 360 187

Table 2:
204 Test1
347 Test2
470 Test3
360 Test4
187 Test5

I want my select statement to return this:
1 Test1 Test2 Test3 Test4 Test5
2 Test2 Test3 Test1 Test4 Test5

Thanks in advance!
Mal
 
Try attaching Table 2 multiple times, eg
Code:
SELECT
 t2_2.field2name
,t2_3.field2name
,t2_4.field2name
,t2_5.field2name
FROM
table1 AS t1
LEFT JOIN table2 AS t2_2 ON t1.field2_id=t2_2.field1_id
LEFT JOIN table2 AS t2_3 ON t1.field3_id=t2_3.field1_id
LEFT JOIN table2 AS t2_4 ON t1.field4_id=t2_4.field1_id
LEFT JOIN table2 AS t2_5 ON t1.field5_id=t2_5.field1_id

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top