2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SQL】AccessのSQLで3つ4つテーブルを結合する【Access】

Posted at

AccessのSQLでは、2つのテーブルを結合する場合は普通にLEFT JOINするだけです。

ところが3つ以上のテーブルを普通に結合しようとするとエラーになります。

なんだこれと思って調べてみると、AccessのSQLだけ一筋縄ではいかないようです。

分かりにくくて難しいです…

3つのテーブルを結合

3つのテーブルを結合(LEFT JOIN)する場合はこうなります。

SELECT *
FROM (TABLE01 AS T1
LEFT JOIN TABLE02 AS T2 ON (T2.No = T1.No))
LEFT JOIN TABLE03 AS T3 ON (T3.No = T2.No)
;

1つ目のテーブルと2つ目のテーブルをカッコでくくってやる必要があるようですね。

4つのテーブルを結合

4つのテーブルを結合する場合だとこうなります。

SELECT *
FROM ((TABLE01 AS T1
LEFT JOIN TABLE02 AS T2 ON (T2.No = T1.No))
LEFT JOIN TABLE03 AS T3 ON (T3.No = T2.No))
LEFT JOIN TABLE04 AS T4 ON (T4.No = T3.No)
;

5つのテーブルを結合

5つのテーブルを結合する場合はこうなります。
これ以降は括弧がどんどん増えていくことになります。

SELECT *
FROM (((TABLE01 AS T1
LEFT JOIN TABLE02 AS T2 ON (T2.No = T1.No))
LEFT JOIN TABLE03 AS T3 ON (T3.No = T2.No))
LEFT JOIN TABLE04 AS T4 ON (T4.No = T3.No))
LEFT JOIN TABLE05 AS T5 ON (T5.No = T4.No)
;
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?