这似乎应该很容易,但我还没有设法创建正确的查询或找到等效的情况。假设我有 3 个要加入的表:Artist、Album和Track。(实际上我没有这些表,但我正在使用的表具有等效关系。)
每个artist可以有很多albums,每个album可以有很多tracks。
该artist表已编入索引artist_id并包含有关艺术家的信息(name、address、phone number等)。
该album表在artist_id和上编制索引album_id。它包含专辑名称和其他信息(recording date、location等)。
该track表在artist_id、album_id和上编制索引track_nbr。它包含轨道的名称、长度等。
我想要做的是创建一个看起来像这样的报告:
Artist|Album|Track#|Track Name
The Wizard of Frobozz|Zork I|01|The White House
The Wizard of Frobozz|Zork I|02|Eaten by a Grue
The Wizard of Frobozz|Zork I|03|Take the Sword
The Wizard of Frobozz|Zork I|04|Don't Forget the Lantern
The Wizard of Frobozz|Zork II|01|Waiting for the Volcano Gnome
The Wizard of Frobozz|Zork II|02|On the Left Bank of Zork
The Wizard of Frobozz|Zork II|03|In the Oddly Angled Room
我可以加入artist和album表以获取所有表,albums但是当我添加表时,每个组合track只能得到一个。trackalbumartist
这似乎应该工作,但没有:
select a.name, l.album, t.track_nbr, t.track_name
from track t, album l, artist a
where t.artist_id = l.artist_id
and l.artist_id = a.artist_id
and t.album_id = a.album_id
order by a.name, l.album, t.track_nbr;
我的猜测是我需要显式声明一个连接并可能添加括号来对连接进行分组,但我无法找到我可以完全理解的文档来执行此操作。
所以……救命?谢谢!