なんだかGoodVibes

日々の勉強メモです。

【SQLite】テーブルの結合(INNER JOIN、LEFT OUTER JOIN)

こんにちは。
SQLiteメモの続きです。

概要

以下の記事の続きです。

【SQLite】基本操作・データベースの作成 - なんだかGoodVibes

【SQLite】テーブルの作成とデータの追加 - なんだかGoodVibes

【SQLite】データの取得(ODER BY、WHERE、GROUP BY) - なんだかGoodVibes


内部結合(INNER JOIN)

2つのテーブルを結合する

カラムの値が一致するデータだけを取得します。
itemsテーブルにcolorsテーブルを結合すると以下のようになります。

sqlite> SELECT items.Name, items.ColorId, colors.Name
   ...> FROM items
   ...> INNER JOIN colors
   ...> ON items.ColorId = colors.Id;
Name        ColorId     Name
----------  ----------  ----------
Apple       1           Red
Strawberry  1           Red
Orange      2           Orange
Lemon       4           Yellow
Kiwifruit   3           Green
Banana      4           Yellow
Tomato      1           Red
Pumpkin     4           Yellow
Spinach     3           Green
GreenPeppe  3           Green


INNER JOINの場合、一致したデータだけを取得するので
先に記載するテーブルを逆にしても同じ結果になります。

sqlite> SELECT items.Name, items.ColorId, colors.Name
   ...> FROM colors
   ...> INNER JOIN items
   ...> ON colors.Id = items.ColorId;
Name        ColorId     Name
----------  ----------  ----------
Apple       1           Red
Strawberry  1           Red
Orange      2           Orange
Lemon       4           Yellow
Kiwifruit   3           Green
Banana      4           Yellow
Tomato      1           Red
Pumpkin     4           Yellow
Spinach     3           Green
GreenPeppe  3           Green
3つのテーブルを結合する

itemsテーブルにcolors、typesテーブルを結合します。

sqlite> SELECT items.Name, items.ColorId, colors.Name, items.TypeId, types.Name
   ...> FROM items
   ...> INNER JOIN colors
   ...> ON items.ColorId = colors.Id
   ...> INNER JOIN types
   ...> ON items.TypeId = types.Id;
Name        ColorId     Name        TypeId      Name
----------  ----------  ----------  ----------  ----------
Apple       1           Red         1           Fruit
Strawberry  1           Red         1           Fruit
Orange      2           Orange      1           Fruit
Lemon       4           Yellow      1           Fruit
Kiwifruit   3           Green       1           Fruit
Banana      4           Yellow      1           Fruit
Tomato      1           Red         2           Vegetables
Pumpkin     4           Yellow      2           Vegetables
Spinach     3           Green       2           Vegetables
GreenPeppe  3           Green       2           Vegetables


外部結合(LEFT OUTER JOIN)

2つのテーブルを結合する

指カラムの値が一致するデータに加えて
先に書かれたテーブルにしかなかったカラム値も取得します。
itemsテーブルにcolorsテーブルを結合すると以下のようになります。

sqlite> SELECT items.Name, items.ColorId, colors.Name
   ...> FROM items
   ...> LEFT OUTER JOIN colors
   ...> ON items.ColorId = colors.Id;
Name        ColorId     Name
----------  ----------  ----------
Apple       1           Red
Strawberry  1           Red
Orange      2           Orange
Lemon       4           Yellow
Kiwifruit   3           Green
Banana      4           Yellow
Tomato      1           Red
Pumpkin     4           Yellow
Spinach     3           Green
GreenPeppe  3           Green


INNER JOINと異なり、先にどのテーブルを記載するかで結果が異なります。
先ほどと逆にcolorsテーブルにitemsテーブルを結合してみると以下のようになります。

sqlite> SELECT items.Name, items.ColorId, colors.Name
   ...> FROM colors
   ...> LEFT OUTER JOIN items
   ...> ON colors.Id = items.ColorId;
Name        ColorId     Name
----------  ----------  ----------
Apple       1           Red
Strawberry  1           Red
Tomato      1           Red
Orange      2           Orange
GreenPeppe  3           Green
Kiwifruit   3           Green
Spinach     3           Green
Banana      4           Yellow
Lemon       4           Yellow
Pumpkin     4           Yellow
                        Bule
                        Purple

今度はcolorsテーブルにしかない値も取得されました。

3つのテーブルを結合する

itemsテーブルにcolors、typesテーブルを結合します。

sqlite> SELECT items.Name, items.ColorId, colors.Name, items.TypeId, types.Name
   ...> FROM items
   ...> LEFT OUTER JOIN colors
   ...> ON items.ColorId = colors.Id
   ...> LEFT OUTER JOIN types
   ...> ON items.TypeId = types.Id;
Name        ColorId     Name        TypeId      Name
----------  ----------  ----------  ----------  ----------
Apple       1           Red         1           Fruit
Strawberry  1           Red         1           Fruit
Orange      2           Orange      1           Fruit
Lemon       4           Yellow      1           Fruit
Kiwifruit   3           Green       1           Fruit
Banana      4           Yellow      1           Fruit
Tomato      1           Red         2           Vegetables
Pumpkin     4           Yellow      2           Vegetables
Spinach     3           Green       2           Vegetables
GreenPeppe  3           Green       2           Vegetables



以上です。