This seems as if it should be easy but I haven’t managed to create the proper query or to find an equivalent situation. Let’s say I have 3 tables that I’d like to join: Artist, Album, and Track. (Actually I don’t have these tables but the tables I’m working with have equivalent relationships.)
Each artist can have many albums and each album can have many tracks.
The artist table is indexed on artist_id and contains information about the artist (name, address, phone number, and such).
The album table is indexed on artist_id and album_id. It contains the name of the album and other information (recording date, location, etc).
The track table is indexed on artist_id, album_id, and track_nbr. It contains the name of the track, length, and such.
What I’m trying to do is create a report that would look like this:
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
I can join the artist and album tables to get all of the albums but when I add the track table, I get only one track per album–artist combination.
This seems like it should work but doesn’t:
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;
My guess is that I need to explicitly declare a join and possibly add parentheses to group the joins but I haven’t been able to find documentation that I can fully comprehend to do this.
So … Help? Thanks!
Database design-related question: Why do you have
artist_idin thetracktable? Theartist_idis already defined through thealbumthat this track belongs to.