Apologies if this question has already been answered. Couldn't find the answer myself.
I'm trying to work out the SQL to extract data from one table which is being referenced from another table. My explanation might be terrible due to looking at it way too long, so see my tables below:
Table 1 | Table 2 |
---|---|
(PK) ID (int) | (PK) ID (int) |
Att_1 (str) | (FK) Att_1 (int) |
Att_2 (str) | (FK) Att_2 (int) |
Att_3 (str) | Att_3 (str) |
Essentially I'm trying to select all from table 2, but table2.att_1 and table2.att_2 are foreign keys of table1.id. What I want to achieve is extract the following:
Value of Table2.ID
Table1.Att_1 where Table2.Att_1 = Table1.ID
Table1.Att_1 where Table2.Att_2 = Table1.ID
Value of Table2.Att_3
The final SQL will be used in a PHP script, so if its not possible to do this in a single line of SQL, I can split the requests up one by one. Just looking for some good old CPD.