English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
DISTINCTとCOUNTを一緒にMySQLクエリで使用できます。まず、テーブルを作成します。CREATEコマンドはテーブルを作成するために使用されます。
mysql> create table DistCountDemo - > ( - > id int, - > name varchar(100), - > age int - > );
INSERT命令の助けでレコードを挿入します。
mysql> insert into DistCountDemo values(1,'John',23); mysql> insert into DistCountDemo values(2,'Bob',24); mysql> insert into DistCountDemo values(3,'John',23); mysql> insert into DistCountDemo values(4,'Carol',23);
SELECT文の助けですべてのレコードを表示します。
mysql> select *from DistCountDemo;
以下は出力。
+------+-------+------+ | id | name | age | +------+-------+------+ | 1 | John | 23 | | 2 | Bob | 24 | | 3 | John | 23 | | 4 | Carol | 23 | +------+-------+------+ 4 rows in set (0.00 sec)
COUNTとDISTINCTを使用して23歳の学生の人数。
mysql> SELECT COUNT(DISTINCT name) from DistCountDemo WHERE age=23;
以下は出力。
+----------------------+ | COUNT(DISTINCT name) | +----------------------+ | 2 | +----------------------+ 1 row in set (0.05 sec)