如何从 join table table 派生数据
How to derive a data from join table table
我有 3 个 table 如下。
现在从 Sell_Table 开始,我如何推导出 Country_Cod?
我如何使用 Prod_Table 作为其他两个 table 之间的桥梁?
select count(Sell_Table.id), Country_Cod, avg(Sell_Table.Ammount)
from sell_table
left join ?
group by Country_Cod
谢谢
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 |
+-----+------------+----------+
您似乎想要两个连接:
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
我有 3 个 table 如下。 现在从 Sell_Table 开始,我如何推导出 Country_Cod? 我如何使用 Prod_Table 作为其他两个 table 之间的桥梁?
select count(Sell_Table.id), Country_Cod, avg(Sell_Table.Ammount)
from sell_table
left join ?
group by Country_Cod
谢谢
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 | +-----+------------+----------+
您似乎想要两个连接:
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