15
9

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 1 year has passed since last update.

SQL | COUNT(DISTINCT column_name) は「同じ値の種類数」をカウントする

Last updated at Posted at 2018-02-27

初歩。

テーブル例

こんなテーブルがある場合。

mysql> SELECT * FROM scores ORDER BY name;
+-------+--------+-------+
| name  | sex    | score |
+-------+--------+-------+
| Alice | female |    60 |
| Bob   | male   |    70 |
| Carol | female |    70 |
| David | male   |    80 |
| Eric  | male   |    80 |
+-------+--------+-------+

sex には male / famale の二種類がある。

mysql> SELECT COUNT(DISTINCT(sex)) AS sex_kind FROM scores;

+----------+
| sex_kind |
+----------+
|        2 |
+----------+

score には 60点 / 70点 / 80点の三種類がある。

mysql> SELECT COUNT(DISTINCT(score)) AS score_kind FROM scores;

+------------+
| score_kind |
+------------+
|          3 |
+------------+

環境

  • mysql Ver 14.14 Distrib 5.5.56, for osx10.12 (x86_64) using EditLine wrapper

参考

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

15
9
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
15
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?