LoginSignup
0
0

More than 3 years have passed since last update.

MySQLの基本的な関数

Posted at

COUNT関数

行数を取得することができる。

スクリーンショット 2020-09-23 17.41.35.png

-- count関数の引数に*を渡した場合、nullのレコードもカウントされる
select count(*) from Shohin;

スクリーンショット 2020-09-23 17.46.13.png

-- 引数で列名を渡した場合、対象の列がnullのレコードはカウントされない
select count(shiire_tanka) from Shohin;

スクリーンショット 2020-09-23 17.51.18.png

重複を除いてカウントしたい場合、distinctを使用する。

select count(distinct shohin_bunrui) from Shohin;

スクリーンショット 2020-09-23 18.36.41.png

SUM関数

合計を取得することができる。
また、集約関数は引数に列名をとった場合、計算前にnullを除外する。
※count(*)は例外的にnullを除外しない

select sum(shiire_tanka) from Shohin;

スクリーンショット 2020-09-23 18.17.04.png

AVG関数

平均値を取得することができる。

select avg(shiire_tanka) from Shohin;

スクリーンショット 2020-09-23 18.24.55.png

MAX関数、MIN関数

最大値、最小値を取得することができる。
構文はSUM関数、AVG関数と同じだが、
SUM関数、AVG関数は数値型の列に対してのみ使用できる点に対し、
MAX関数、MIN関数は原則的にどのような型の列にも使用できる。

-- 数値型の列を指定
select min(shiire_tanka) as "最小値", max(shiire_tanka) as "最大値" from Shohin;
-- 日付型の列を指定
select min(torokubi) as "最小値", max(torokubi) as "最大値" from Shohin;

スクリーンショット 2020-09-23 18.33.12.png

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