25
22

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.

SQL【書き順と実行の順序】

Last updated at Posted at 2019-05-10

#書き順
SELECT
FROM JOIN
ON
WHERE
GROUPBY
HAVING
ORDERBY

#実行の順序
FROM(どのテーブルかを選ぶ)
ON(テーブル同士がどのカラムで結合するかを選ぶ)
JOIN(結合するテーブルを選ぶ)
WHERE(選択する行を選ぶ)
GROUPBY(行ごとにまとめる。どの列でまとめるかを決める)
HAVING(GROUPBYでまとめたもののうち条件に合うものを抽出する)
SELECT(結果に出すカラムを選ぶ)
DISTINCT(重複をなくして結果を出す)
ORDERBY(順番を決める。どのカラムを昇順or降順にするか)
TOP(LIMIT)(上位何件を結果として出すか)

#SQL文の書き方備忘録
##複数のテーブルを結合するときの書き方
SELECT カラム名
FROM テーブル名1
JOIN テーブル名2
ON テーブル名1.結合キー列 = テーブル名2.結合キー列
JOIN テーブル名3
ON* テーブル名X.結合キー列 = テーブル名3.結合キー列

#最後に+参考
railsを勉強していてモデルを操作する際になにが起きてるか気になったので調べました。
https://dev.classmethod.jp/server-side/db/difference-where-and-having/ (HAVINGとWHEREの違い)
https://qiita.com/suzukito/items/edcd00e680186f2930a8 (評価順序)

25
22
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
25
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?