エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

2
3

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.

phpMyAdmin リレーション

Posted at

2つのテーブルを繋げるリレーション

phpMyAdminのSQLで2つのテーブルを繋げるリレーションを忘れないように記事で残しておきたいと思います。
2019-10-23 (2).png
2019-10-23 (3).png
上から順番に1番目がmy_itemsテーブル、2番目がmakersテーブルです。

SELECT * FROM makers, my_items WHERE my_items.id=1 and makers.id=my_items.maker_id
上記の一文がリレーションを使ったSQL文です。
makers, my_itemsの部分で繋げるテーブル2種類の名前をカンマを挟んで記述してます。
WHERE my_items.id=1でmy_itemsテーブルのidが1のデータを抽出してます。テーブル名.カラム名=値で記述しましょう。
and makers.id=my_items.maker_idでmakersテーブルのidカラムとmy_itemsテーブルのmaker_idで2つのテーブルを繋げています。
こちらもテーブル名1.カラム名=テーブル名2.カラム名で記述しましょう。

この一文を実行した結果が下の画像です。
2019-10-23 (5).png
idカラムが2つあるのでわかりにくいですが、my_itemsのmaker_idとmakerのidで繋がってmy_itemsのidが1のデータを抽出してます。

SELECT * FROM makers, my_items WHERE makers.id=my_items.maker_id
因みにmy_itemsテーブルのidが1のデータを抽出する文を省いて上の一文を実行すると
makersテーブルのidカラムとmy_itemsテーブルのmaker_idカラムの値を関連付けて全てのデータを表示出来ます。
2019-10-23 (7).png

2
3
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?