Search code examples
mysqlsqlinner-join

How to derive a data from join table table


I have 3 table as following. Now starting from Sell_Table how can i derive the Country_Cod? How can i use the Prod_Table as a bridge between the other two tables?

select count(Sell_Table.id), Country_Cod, avg(Sell_Table.Ammount)
from sell_table
left join ?
group by Country_Cod

Thanks

1)Country table
+--- -+-------------+
| id  | Country_Cod |
+---- +-------------+
| 101 | IT          |
| 102 | US          |
| 103 | GB          |
| 104 | TR          |
+-----+-------------+
2) Prod_Table
+-----+------------+---------+
| id  | Country_id | Sell__id|
+---- +------------+---------+
| 101 |101         | 101     |
| 102 |102         |         |
| 103 |102         | 102     |
| 104 |103         |         |
+-----+------------+---------+
3) Sell_Table
+-----+------------+----------+
| id  | Prod_id    | Amount   |
+---- +------------+----------+
| 101 |101         | 100      |
| 102 |103         | 200      |
| 103 |107         | 300      |
| 104 |110         | 400      |
+-----+------------+----------+

Solution

  • You seem to want two joins:

    select s.id, c.country_code, s.amount
    from sell s
    inner join prod p on p.id = s.prod_id
    inner join country c on c.id = p.country_id