1
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?

More than 5 years have passed since last update.

【SQL】GROUP BYの使い方自分用

Last updated at Posted at 2020-05-01

GROUP BYを一言で言うと、

カラム名ごとにカラムを計算などをした結果を表示するよ!

と言うことです。

使用するタイミング

主にカラム名ごとに数字(得点など)を合計、平均したい際にgroup byが使われます。

例題

出身国ごとの合計得点数を取得したい。

テーブルは以下の通り。
レコードは省略します。

id name goals height country_id

とりあえず例題を日本語に直すと、以下のようになります。

SELECT カラム名1、カラム名2  #複数指定する
FROM players
group by カラム名2;

そして、

SELECT sum(goals),country_id
FROM players
group by country_id ;

こうなります。

country_idごとにgoalsを足した値を表示するよ!ということになります。

使用するタイミングだけ覚えておけばそこまで難しくない?
カラムごとに数字を計算して、データを抜き出したいタイミングで使用します。

1
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
1
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?