LoginSignup
19
18

More than 5 years have passed since last update.

INNER JOINでエラーメッセージ「Column 'カラム名' in field list is ambiguous」が出た時

Posted at

原因

INNER JOINでテーブル結合する時に、テーブル内で同じカラムがあった場合、select等でそのカラムがどのテーブルに存在するものか指定していないとエラーメッセージ「Column 'カラム名' in field list is ambiguous」が表示される。

原因は単純にカラムが重複してしまい、どのテーブルを参照すればいいのか分からないから。

select id,status,
from テーブルA INNER JOIN テーブルB on テーブルA.カラム① = テーブルB.カラム①
where id is not null and status = 1

上記で実行すると→Column 'id' in field list is ambiguous

※カラム①のカラム名が同じ意味合いなのにテーブルで違う名前の場合もあるが、それはそれで、そのまま書けばOK

テーブルA

id status Category
1 1 A
2 1 A
3 3 B
4 4 C
5 1 C
null 2 C

テーブルB

id status type
1 1 GJ
2 1 Go
3 0 Go
4 0 Go
5 0 GJ
null 0 GJ

テーブルAとテーブルBのidとstatusが重複してる

解決

SQL文内の重複するカラム全てにどのテーブルのカラムなのか追記する。

select id,status,
from テーブルA INNER JOIN テーブルB on テーブルA.カラム① = テーブルB.カラム①
where テーブルA.id is not null and テーブルB.status = 1

以上。

19
18
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
19
18