我应该实施什么样的连接才能接收我想要获得的输出?

What kind of join should I implement in order to receive the output I am trying to get?

注意:只是一道练习题..不是在寻找免费的家庭作业答案。

我的练习题要求按照飞机年份的升序按飞机年份报告飞行次数。这需要连接两个 table,航班 table 和飞机 table。我相信 SQL 应该相对简单,我认为我的主要问题是我目前的 select 声明含糊不清。我查看了不同的连接方法,包括显式连接和隐式连接,还尝试了左连接,但没有成功。

如果需要更多 table 信息,我可以分享。两个 table 共享的列是年份。

另外,这里很新,所以如果有什么不满意或不正确的地方 post,请告诉我。

select *,  
count(*) as n_flights  
from flights, planes 
where flights.year = planes.year 
order by planes.year asc 
;

我正在寻找的输出:

我得到的输出:

据推测,航班 table 中有一列引用飞机的主键 table - 让我假设 plane_id:这就是您要用来加入 tables.

然后,您要使用 group by 子句按飞机年进行汇总,并计算每组中的行数:

select p.year, count(*) as n_flights  
from flights f
inner join planes p on p.plane_id = f.plane_id
group by p.year 
order by p.year 
select planes.year, count(*) 
from flights, planes 
where flights.year = planes.year 
group by planes.year
order by planes.year asc