[SOLVED] Joining 3 tables with mySQL

Issue

I have three following tables.

table 1)
| ID_t1  |   name1   | price |
| ------ | --------- | ----- |
|   1    |    AAA    |  100  |
|   2    |    ABB    |  200  |
|   3    |    ACC    |  300  |

table 2)
| ID_t2  |   name2   |
| ------ | --------- |
|   1    |    pig    |
|   2    |    fog    |
|   3    |    dog    |

table 3)
| ID_t3  |   ID_t1   | ID_t2 | quantity |
| ------ | --------- | ----- | -------- |
|   1    |      1    |   1   |    50    |
|   2    |      2    |   1   |    60    |
|   3    |      2    |   2   |   100    |
|   4    |      2    |   3   |   110    |
|   5    |      3    |   2   |   150    |
|   6    |      3    |   3   |   140    |

So I have relation ibfk in table3 with column ID_t1, ID_t2.

I want to get result in mysql like this.

| ID_t3  |   name1   |  name2 | quantity |
| ------ | --------- | ------ | -------- |
|   1    |    AAA    |   pig  |    50    |
|   2    |    ABB    |   pig  |    60    |
|   3    |    ABB    |   fog  |   100    |
|   4    |    ABB    |   dog  |   110    |
|   5    |    ACC    |   fog  |   150    |
|   6    |    ACC    |   dog  |   140    |

Solution

Here what exactly you do for your query

SELECT 
table_3.ID_t3,
table_1.name1,
table_2.name2, 
table_3.quantity 
FROM table_3 
JOIN table_1 on table_3.ID_t1 = table_1.ID_t1 
JOIN table_2 ON table_3.ID_t2 = table_2.ID_t2

Answered By – beibei

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *