Inner Join(III)-連接 3 個資料表
你已經看過如何把 2 個資料表連接在一起──album 與 track,以及 album 與 artist。在這個練習中,你會把三個資料表都連接起來,取得更完整的結果集。你會繼續使用 INNER JOIN,但需要指定不只一次的連接。
另外要注意,因為 track 和 artist 都有 name 欄位,你需要以資料表名稱作為前綴來「限定(qualify)」要選取的欄位,讓欄位名稱帶上對應的資料表名稱。
本練習屬於課程
SQL Server 入門
練習說明
- 在兩個情況中都為
name欄位加上正確的資料表前綴以進行限定。 - 完成兩個
INNER JOIN子句,將album與track連接,並將artist與album連接。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT track_id,
-- Enter the correct table name prefix when retrieving the name column from the track table
___.name AS track_name,
title as album_title,
-- Enter the correct table name prefix when retrieving the name column from the artist table
___.name AS artist_name
FROM track
-- Complete the matching columns to join album with track, and artist with album
INNER JOIN album on track.___ = album.album_id
INNER JOIN artist on album.artist_id = artist.___;