0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

(PostgreSQL)JOINのLEFT・RIGHT・INNERの結果早見表

Posted at

サンプル

tableA

id name
1 Alice
2 Bob
3 Carol

tableB

id city
1 Tokyo
2 New York
4 London

JOIN していく

各テーブルのid同士でJOINしてみる

INNER JOIN

INNER JOIN
SELECT * FROM tableA INNER JOIN tableB ON tableA.id = tableB.id;

結果

tableA.id name tableB.id city
1 Alice 1 Tokyo
2 Bob 2 New York

重複するIDのレコードのみセレクトされる

LEFT JOIN

LEFT JOIN
SELECT * FROM tableA LEFT JOIN tableB ON tableA.id = tableB.id;

結果

tableA.id name tableB.id city
1 Alice 1 Tokyo
2 Bob 2 New York
3 Carol NULL NULL

ONの条件式で左側のテーブルのレコードは全てセレクトされる
右側のテーブルに該当のIDがなければNULLになる

RIGHT JOIN

RIGHT JOIN
SELECT * FROM tableA RIGHT JOIN tableB ON tableA.id = tableB.id;

結果

tableA.id name tableB.id city
1 Alice 1 Tokyo
2 Bob 2 New York
NULL NULL 4 London

LEFTの逆

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?