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 5 years have passed since last update.

MySQLでINNER JOINする

0
Last updated at Posted at 2019-10-02

はじめに

この記事は勉強用として試したことまとめたものです。

INNER JOIN(内部結合)とは

  • テーブルとテーブルを結合するためのSQL文である。
  • 条件に一致するレコードのみを抽出する。

テストデータ作成(名前テーブル)

CREATE TABLE pokemon_name (
  book_no int NOT NULL, -- 図鑑番号
  type_no varchar(255), -- タイプ番号
  name varchar(255),    -- 名前
  PRIMARY KEY (book_no)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

-- テストデータ投入
INSERT INTO pokemon_name VALUES('001','A01','フシギダネ');
INSERT INTO pokemon_name VALUES('002','A01','フシギソウ');
INSERT INTO pokemon_name VALUES('003','A01','フシギバナ');
INSERT INTO pokemon_name VALUES('004','A02','ヒトカゲ');
INSERT INTO pokemon_name VALUES('005','A02','リザード');
INSERT INTO pokemon_name VALUES('006','A02','リザードン');
INSERT INTO pokemon_name VALUES('007','A03','ゼニガメ');

テストデータ作成(タイプ名テーブル)

CREATE TABLE pokemon_type (
  no char(5),             -- タイプ番号
  type_name varchar(255), -- タイプ名
  PRIMARY KEY (no)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

-- テストデータ投入
INSERT INTO pokemon_type VALUES('A01','くさ');
INSERT INTO pokemon_type VALUES('A02','ほのお');
INSERT INTO pokemon_type VALUES('A03','みず');

SQL実行

SELECT * FROM pokemon_name pn
INNER JOIN pokemon_type pt ON pn.type_no = pt.no;

実行結果

book_no|type_no|name |no |type_name|
-------|-------|-----|---|---------|
      1|A01    |フシギダネ|A01|くさ       |
      2|A01    |フシギソウ|A01|くさ       |
      3|A01    |フシギバナ|A01|くさ       |
      4|A02    |ヒトカゲ |A02|ほのお      |
      5|A02    |リザード |A02|ほのお      |
      6|A02    |リザードン|A02|ほのお      |
      7|A03    |ゼニガメ |A03|みず       |

同じ表を結合させてみた

SELECT * FROM pokemon_name pn
INNER JOIN pokemon_type pt1 ON pn.type_no = pt1.no
INNER JOIN pokemon_type pt2 ON pn.type_no = pt2.no
INNER JOIN pokemon_type pt3 ON pn.type_no = pt3.no
INNER JOIN pokemon_type pt4 ON pn.type_no = pt4.no
INNER JOIN pokemon_type pt5 ON pn.type_no = pt5.no;

実行結果

book_no|type_no|name |no |type_name|no |type_name|no |type_name|no |type_name|no |type_name|
-------|-------|-----|---|---------|---|---------|---|---------|---|---------|---|---------|
      1|A01    |フシギダネ|A01|くさ       |A01|くさ       |A01|くさ       |A01|くさ       |A01|くさ       |
      2|A01    |フシギソウ|A01|くさ       |A01|くさ       |A01|くさ       |A01|くさ       |A01|くさ       |
      3|A01    |フシギバナ|A01|くさ       |A01|くさ       |A01|くさ       |A01|くさ       |A01|くさ       |
      4|A02    |ヒトカゲ |A02|ほのお      |A02|ほのお      |A02|ほのお      |A02|ほのお      |A02|ほのお      |
      5|A02    |リザード |A02|ほのお      |A02|ほのお      |A02|ほのお      |A02|ほのお      |A02|ほのお      |
      6|A02    |リザードン|A02|ほのお      |A02|ほのお      |A02|ほのお      |A02|ほのお      |A02|ほのお      |
      7|A03    |ゼニガメ |A03|みず       |A03|みず       |A03|みず       |A03|みず       |A03|みず       |

参考

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?