UNION
ALL
JOIN
ON
LEFT JOIN
imdb
資料庫中,哪幾部電影 Tom Hanks 有演出?¶actors
資料表查詢 Tom Hanks 的演員編號是多少。casting
資料表查詢。movies
資料表查詢。UNION
垂直合併¶A SELECT statement
UNION
Another SELECT statement
SELECT director AS my_favorites
FROM movies
WHERE director IN ('Christopher Nolan', 'Steven Spielberg')
UNION
SELECT name
FROM actors
WHERE name IN ('Tom Hanks', 'Leonardo DiCaprio');
my_favorites |
---|
Christopher Nolan |
Leonardo DiCaprio |
Steven Spielberg |
Tom Hanks |
UNION
的注意事項¶ORDER BY
只能放在 UNION
之後。SELECT director AS my_favorites
FROM movies
WHERE director IN ('Christopher Nolan', 'Steven Spielberg')
UNION
SELECT name,
id -- 垂直合併的欄位數不相同
FROM actors
WHERE name IN ('Tom Hanks', 'Leonardo DiCaprio');
ORDER BY
只能放在 UNION
之後¶SELECT director AS my_favorites
FROM movies
WHERE director IN ('Christopher Nolan', 'Steven Spielberg')
ORDER BY my_favorites
UNION
SELECT name
FROM actors
WHERE name IN ('Tom Hanks', 'Leonardo DiCaprio');
SELECT director AS my_favorites
FROM movies
WHERE director IN ('Christopher Nolan', 'Steven Spielberg');
SELECT name
FROM actors
WHERE name IN ('Tom Hanks', 'Leonardo DiCaprio')
ORDER BY my_favorites;
my_favorites |
---|
Christopher Nolan |
Steven Spielberg |
Christopher Nolan |
Steven Spielberg |
Christopher Nolan |
Christopher Nolan |
Christopher Nolan |
Steven Spielberg |
Christopher Nolan |
Steven Spielberg |
Christopher Nolan |
Steven Spielberg |
Steven Spielberg |
UNION ALL
¶A SELECT statement
UNION ALL
Another SELECT statement
SELECT director AS my_favorites
FROM movies
WHERE director IN ('Christopher Nolan', 'Steven Spielberg')
UNION ALL
SELECT name
FROM actors
WHERE name IN ('Tom Hanks', 'Leonardo DiCaprio')
ORDER BY my_favorites;
my_favorites |
---|
Christopher Nolan |
Christopher Nolan |
Christopher Nolan |
Christopher Nolan |
Christopher Nolan |
Christopher Nolan |
Christopher Nolan |
Leonardo DiCaprio |
Steven Spielberg |
Steven Spielberg |
Steven Spielberg |
Steven Spielberg |
Steven Spielberg |
Steven Spielberg |
Tom Hanks |
covid19
資料庫的實體關係圖¶imdb
資料庫的實體關係圖¶nba
資料庫的實體關係圖¶twElection2020
資料庫的實體關係圖¶JOIN
水平合併¶因為是「水平」合併,在 FROM
後的資料表被稱為「左表格」、JOIN
後的資料表被稱為「右表格」。
SELECT left_table.column_names,
right_table.column_names
FROM left_table
JOIN right_table
ON left_table.join_key = right_table.join_key;
JOIN
水平合併 movies
與 casting
¶SELECT movies.title,
casting.actor_id
FROM movies -- 左表格
JOIN casting -- 右表格
ON movies.id = casting.movie_id
WHERE movies.title = 'The Shawshank Redemption';
title | actor_id |
---|---|
The Shawshank Redemption | 2853 |
The Shawshank Redemption | 2097 |
The Shawshank Redemption | 333 |
The Shawshank Redemption | 3029 |
The Shawshank Redemption | 533 |
The Shawshank Redemption | 1042 |
The Shawshank Redemption | 1923 |
The Shawshank Redemption | 1319 |
The Shawshank Redemption | 1370 |
The Shawshank Redemption | 1724 |
The Shawshank Redemption | 2151 |
The Shawshank Redemption | 363 |
The Shawshank Redemption | 659 |
The Shawshank Redemption | 1530 |
The Shawshank Redemption | 1553 |
JOIN
水平合併 movies
、casting
與 actors
¶SELECT movies.title,
casting.ord,
actors.name
FROM movies -- 左表格
JOIN casting -- 右一表格
ON movies.id = casting.movie_id
JOIN actors -- 右二表格
ON casting.actor_id = actors.id
WHERE movies.title = 'The Shawshank Redemption';
title | ord | name |
---|---|---|
The Shawshank Redemption | 1 | Tim Robbins |
The Shawshank Redemption | 2 | Morgan Freeman |
The Shawshank Redemption | 3 | Bob Gunton |
The Shawshank Redemption | 4 | William Sadler |
The Shawshank Redemption | 5 | Clancy Brown |
The Shawshank Redemption | 6 | Gil Bellows |
The Shawshank Redemption | 7 | Mark Rolston |
The Shawshank Redemption | 8 | James Whitmore |
The Shawshank Redemption | 9 | Jeffrey DeMunn |
The Shawshank Redemption | 10 | Larry Brandenburg |
The Shawshank Redemption | 11 | Neil Giuntoli |
The Shawshank Redemption | 12 | Brian Libby |
The Shawshank Redemption | 13 | David Proval |
The Shawshank Redemption | 14 | Joseph Ragno |
The Shawshank Redemption | 15 | Jude Ciccolella |
JOIN
水平合併資料表時的注意事項¶舉例來說,在 movies
與 actors
兩個資料表中都有 id
欄位。
movies.id
指的是電影編號。actors.id
指的是演員編號。SELECT title,
ord,
name
FROM movies
JOIN casting
ON id = movie_id
JOIN actors
ON actor_id = id
WHERE title = 'The Shawshank Redemption';
SELECT subquery_movies.title,
casting.ord,
actors.name
FROM (SELECT id,
title,
director
FROM movies
WHERE title IN ('The Shawshank Redemption', 'Forrest Gump')) AS subquery_movies -- 左表格
JOIN casting -- 右一表格
ON subquery_movies.id = casting.movie_id
JOIN actors -- 右二表格
ON casting.actor_id = actors.id
ORDER BY ord;
title | ord | name |
---|---|---|
The Shawshank Redemption | 1 | Tim Robbins |
Forrest Gump | 1 | Tom Hanks |
The Shawshank Redemption | 2 | Morgan Freeman |
Forrest Gump | 2 | Rebecca Williams |
The Shawshank Redemption | 3 | Bob Gunton |
Forrest Gump | 3 | Sally Field |
The Shawshank Redemption | 4 | William Sadler |
Forrest Gump | 4 | Michael Conner Humphreys |
The Shawshank Redemption | 5 | Clancy Brown |
Forrest Gump | 5 | Harold G. Herthum |
The Shawshank Redemption | 6 | Gil Bellows |
Forrest Gump | 6 | George Kelly |
The Shawshank Redemption | 7 | Mark Rolston |
Forrest Gump | 7 | Bob Penny |
The Shawshank Redemption | 8 | James Whitmore |
Forrest Gump | 8 | John Randall |
The Shawshank Redemption | 9 | Jeffrey DeMunn |
Forrest Gump | 9 | Sam Anderson |
The Shawshank Redemption | 10 | Larry Brandenburg |
Forrest Gump | 10 | Margo Moorer |
The Shawshank Redemption | 11 | Neil Giuntoli |
Forrest Gump | 11 | Ione M. Telech |
The Shawshank Redemption | 12 | Brian Libby |
Forrest Gump | 12 | Christine Seabrook |
The Shawshank Redemption | 13 | David Proval |
Forrest Gump | 13 | John Worsham |
The Shawshank Redemption | 14 | Joseph Ragno |
Forrest Gump | 14 | Peter Dobson |
The Shawshank Redemption | 15 | Jude Ciccolella |
Forrest Gump | 15 | Siobhan Fallon Hogan |
SELECT two_movies.title,
two_castings.actor_id
-- 左表格是刺激1995與阿甘正傳的電影資料
FROM (SELECT *
FROM movies
WHERE title IN ('The Shawshank Redemption', 'Forrest Gump')) AS two_movies
-- 右表格是刺激1995與黑暗騎士的演員名單資料
JOIN (SELECT *
FROM casting
WHERE movie_id IN (1, 4)) AS two_castings
ON two_movies.id = two_castings.movie_id
ORDER BY two_movies.title;
title | actor_id |
---|---|
The Shawshank Redemption | 2853 |
The Shawshank Redemption | 2097 |
The Shawshank Redemption | 333 |
The Shawshank Redemption | 3029 |
The Shawshank Redemption | 533 |
The Shawshank Redemption | 1042 |
The Shawshank Redemption | 1923 |
The Shawshank Redemption | 1319 |
The Shawshank Redemption | 1370 |
The Shawshank Redemption | 1724 |
The Shawshank Redemption | 2151 |
The Shawshank Redemption | 363 |
The Shawshank Redemption | 659 |
The Shawshank Redemption | 1530 |
The Shawshank Redemption | 1553 |
LEFT JOIN
¶SELECT left_table.column_names,
right_table.column_names
FROM table_name AS left_table
LEFT JOIN table_name AS right_table
ON left_table.join_key = right_table.join_key;
SELECT two_movies.title,
two_castings.actor_id
-- 左表格是刺激1995與阿甘正傳的電影資料
FROM (SELECT *
FROM movies
WHERE title IN ('The Shawshank Redemption', 'Forrest Gump')) AS two_movies
-- 右表格是刺激1995與黑暗騎士的名單資料
LEFT JOIN (SELECT *
FROM casting
WHERE movie_id IN (1, 4)) AS two_castings
ON two_movies.id = two_castings.movie_id
ORDER BY two_movies.title;
title | actor_id |
---|---|
Forrest Gump | NULL |
The Shawshank Redemption | 333 |
The Shawshank Redemption | 363 |
The Shawshank Redemption | 533 |
The Shawshank Redemption | 659 |
The Shawshank Redemption | 1042 |
The Shawshank Redemption | 1319 |
The Shawshank Redemption | 1370 |
The Shawshank Redemption | 1530 |
The Shawshank Redemption | 1553 |
The Shawshank Redemption | 1724 |
The Shawshank Redemption | 1923 |
The Shawshank Redemption | 2097 |
The Shawshank Redemption | 2151 |
The Shawshank Redemption | 2853 |
The Shawshank Redemption | 3029 |
UNION
垂直合併的注意事項ORDER BY
只能放在 UNION
之後。UNION ALL
。JOIN
水平合併的注意事項LEFT JOIN
。/*截至目前學起來的 SQL 有哪些?
SQL 寫作順序必須遵從標準 SQL 的規定。*/
SELECT column_names -- 選擇哪些欄位
FROM left_table -- 從哪個資料庫的資料表
JOIN right_table -- 與哪個資料表水平合併
ON left_table.join_key = right_table.join_key
WHERE conditions -- 篩選哪些觀測值
GROUP BY column_names -- 指定依照哪個變數分組
HAVING conditions -- 篩選哪些分組聚合的結果
UNION SELECT statement -- 與哪段 SQL 垂直合併
ORDER BY column_names -- 指定依照哪個變數排序
LIMIT m; -- 查詢結果顯示前 m 列就好