iT邦幫忙

2022 iThome 鐵人賽

DAY 28
0

Hihi,打給厚!

繼昨天認識、練習了CTE 後,還是沒用此解出情境4,也許是因為沒把CTE JOIN 到正確的query 上,囧

在想自己得乖乖正視心魔,重新認識JOIN 的概念再回頭嘗試,那讓我們開始吧!


首先,先讓我們看看PostgreSQL documentation對JOIN 的解釋:

Queries that access multiple tables (or multiple instances of the same table) at one time are called join queries. They combine rows from one table with rows from a second table, with an expression specifying which rows are to be paired.

翻譯蒟蒻:JOIN queirs意指對數個資料表query 或一次性對同個資料表的數個實體query,其是透過指定兩資料表要配對的欄位,進而將兩個資料表的列組合在一起

有點拗口,且不知道為什麼是指定row 而不是column -_-

只好先跟著PostgreSQL Tutorials: Joins 來理解:

首先先跟著建立兩個資料表:

CREATE TABLE basket_a (
    id INT PRIMARY KEY,
    fruit_a VARCHAR (100) NOT NULL
);

CREATE TABLE basket_b (
    id INT PRIMARY KEY,
    fruit_b VARCHAR (100) NOT NULL
);

INSERT INTO basket_a (id_a, fruit_a)
VALUES
    (1, 'Apple'),
    (2, 'Orange'),
    (3, 'Banana'),
    (4, 'Cucumber');

INSERT INTO basket_b (id_b, fruit_b)
VALUES
    (1, 'Orange'),
    (2, 'Apple'),
    (3, 'Watermelon'),
    (4, 'Pear');

此時會建立兩個資料表:
https://ithelp.ithome.com.tw/upload/images/20221012/20150959TG8P1LOcnB.png
https://ithelp.ithome.com.tw/upload/images/20221012/20150959qs83d2uRkZ.png

當我們將兩個資料表JOIN 起來,並指定fruit_a = fruit_b為配對欄位

SELECT
	id_a,
	fruit_a,
	id_b,
	fruit_b
FROM
	basket_a
	JOIN basket_b ON fruit_a = fruit_b;

會得到這結果:

https://ithelp.ithome.com.tw/upload/images/20221012/20150959WGhOPApKpb.png

這邊的JOIN 即INNER JOIN,會用第一個資料表(basket_a)的fruit_a 欄位與第二個資料表(basket_b)的fruit_b 欄位配對,若是兩者都有的值/紀錄,則會將共有的rows 之資料INNER JOIN 起來,並回傳JOIN 後的新結果集

目前看起來都還能理解,但總共有幾種JOIN 方式呢?
PostgreSQL Tutorial提到有:inner join, left join, right join, full outer join, cross join, natural join, and a special kind of join called self-join.

被翻爛的書頭腦快爆了,所以先來看看圖解想像吧 XD

上述的JOIN 方式以類別來分:

  1. INNER JOIN: inner join, natural join(先列,但還看不懂)
  2. OUTER JOIN: left join, right join, full outer join 等
  3. CROSS JOIN: 又稱為迪笛卡兒積 (Cartesian product),可想成是JOIN 時將其排列組合都列出來,很像以前學過的矩陣?
  4. SELF JOIN: 不懂, too

[圖1]搭配JOIN query & paired keys 的圖解:

https://ithelp.ithome.com.tw/upload/images/20221012/20150959uEXCAXaJBg.png

[圖2]搭配JOIN types 和其稱呼:

https://ithelp.ithome.com.tw/upload/images/20221012/20150959jcGGIUPJpJ.png

註:以上兩圖都有兩個LEFT OUTER JOIN、RIGHT OUTER JOIN,看起來沒有固定的稱呼;以LEFT OUTER JOIN 為例,圖2 是用LEFT Minus JOIN 稱呼,有些是用LEFT JOIN WITH NULL Values 來稱呼的樣子

[圖3]資料表列的圖解-ver1:

https://ithelp.ithome.com.tw/upload/images/20221012/20150959vFRf5RrdAs.png

[圖4]資料表列的圖解-ver2:

https://ithelp.ithome.com.tw/upload/images/20221012/20150959xDP6y3gKu8.png


參考資料:

  1. PostgreSQL Documentation - Join
  2. PostgreSQL Tutorials - Joins
  3. WikiBooks - The different types of SQL join and their outputs as determined by sets
  4. Google Cloud - BigQuery explained: Working with joins, nested & repeated data
  5. Data School - SQL Join Types Explained Visually
  6. Joins with Pandas – Data Science Examples

上一篇
Day 27 - SQL: CTE
下一篇
Day 29 - SQL: Join(2)
系列文
SQL rookie 之天天魯一下30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言