查询中的多个内部联接

时间:2019-08-28 13:07:28

标签: sql

Players{
Pid int primary key,
tid int not null references Teams,
name text not null,
age int not null
}

Teams{
tid int primary key,
name text not null,
location not null
}

Possessions{
id int primary key,
pid int not null references Players,
time int not null, //the time the possession started for a player
held int not null //for how much time he had the ball
}

我想创建一个称为Teampasses的视图,我可以在其中选择(passer,passee),如下所示:Passer和passe必须来自同一团队,并且传球的开始时间等于传球的开始时间+ hold(他拥有的时间)球)。到目前为止,我所做的是:

SELECT x.name AS passer,y.name as Pasee
FROM player x
INNER JOIN player y ON x.tid=y.tid
INNER JOIN possesions p ON p.pid=x.pid AND p.pid=y.pid AND ...

...右边AND部分中,我想做类似x.time+x.held=y.time的事情。我怎么能提到两个?

1 个答案:

答案 0 :(得分:0)

我看到您的数据有问题:

您的Possessions表仅具有Players的{​​{1}}表的单个外键。它还需要包含passer中的Pid。否则,无法过滤掉给定Passee的{​​{1}} passer上的哪个玩家是team

我建议如下更改Passee表:

Possession

进行此更改后,您的数据将可用,查询变得无足轻重。

相关问题