LoginSignup
0
2

More than 5 years have passed since last update.

【SQL】テーブル結合

Posted at

テーブル結合

■内部結合
→結合するデータのみを出力する。結合しないデータは、出力されない。
(NULLとNULLは等しくないので含まれない)
・join

■外部結合
→メインとするテーブルの全行を出力する。(NULL含む)
・left join(左外部結合)
・right join(右外部結合)
・full join(完全外部結合)

内部結合

join

↓基本の形

select 出力するカラム名
from テーブルA
join テーブルB
on 両テーブルの結合する条件

↓イメージ

select A.name, B.name
from bookstore as A
join author as B
on bookstore.authorid = author.id

外部結合

left join

メインテーブルとの結合相手が見当たらなくても、NULLであっても、必ずメインテーブルの全行を出力する。
↓基本の形

select 出力するカラム名
from テーブルA
left join テーブルB
on 両テーブルの結合する条件

↓イメージ

select A.name, B.name
from bookstore as A
left join author as B
on bookstore.authorid = author.id

right join

メインテーブルとの結合相手が見当たらなくても、NULLであっても、必ず結合テーブルの全行を出力する。
↓基本の形

select 出力するカラム名
from テーブルA
right join テーブルB
on 両テーブルの結合する条件

↓イメージ

select A.name, B.name
from bookstore as A
right join author as B
on bookstore.authorid = author.id

full join

メインテーブルとの結合相手が見当たらなくても、NULLであっても、必ずメインテーブルと結合テーブルの全行を出力する。
↓基本の形

select 出力するカラム名
from テーブルA
full join テーブルB
on 両テーブルの結合する条件

↓イメージ

select A.name, B.name
from bookstore as A
full join author as B
on bookstore.authorid = author.id
0
2
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
2