Title: Mysql: Extract Data from 3 Tables using ON clause
Tags: mysql
Question: I have a query related to fetching records from the combination of 3 tables in a way that the returned result will be fetched using the ON clause with the help of foreign keys.
Lets assume, I have three tables named table1, table2, and table3.
Table: table1
```id   name   t2_id   t3_id
11   John   21      31
12   Doe    22      32
```
Table: table2
```id   value
21   ABC-1
22   ABC-2
```
Table: table3
```id   value
31   XYZ-1
32   XYZ-2
```
In table:table1, t2_id and t3_id are foreign keys representing id from table table2 and table3 respectively.
Question:
I want to extract the records from table1 but also get the values from table2 and table 3 using their foreign key present in table1.
Desired Results:
```id   name   t2_value   t3_value
11   John   ABC-1      XYZ-1
12   Doe    ABC-2      XYZ-2
```
What I have done right now:
I have written the below query for doing this task but its not working.
```SELECT table1.id, table1.name, table2.value AS t2_value FROM table1 JOIN table2 ON (table1.t2_id=table2.id);
```
The output of above query will be like this:
```id   name   t2_value
11   John   ABC-1
12   Doe    ABC-2
```
But i want to combine the 3rd table value here too.
Kindly help
```
Important Note: I want to do this using Single MySQL Query.
```


Here is the accepted answer: You're on the right track. You just need to add another ```join``` clause for ```table3``` and add the column you want from it to the select list:
```SELECT table1.id, table1.name, table2.value AS t2_value, table3.value.value AS t3_value
FROM   table1 
JOIN   table2 ON table1.t2_id = table2.id
JOIN   table3 ON table1.t3_id = table3.id
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:08/28/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<mysql>', 'Title': 'Mysql: Extract Data from 3 Tables using ON clause', 'ViewCount': 33, 'id': 63638251, 'user_AboutMe': None, 'user_AccountId': 14400597.0, 'user_DisplayName': 'John Doe', 'user_Reputation': 123.0, 'user_Views': 20.0, 'user_id': 10402390.0}}