English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
次に、列の中で各異なる値の出現回数を取得する例を見てみましょう。まず、テーブルを作成します。
CREATEコマンドはテーブルの作成に使用されます。
mysql> create table DistinctDemo1 - > ( - > id int( - > name varchar(100) - > );
mysql> insert into DistinctDemo1 values(1,'John'); mysql> insert into DistinctDemo1 values(2,'John'); mysql> insert into DistinctDemo1 values(3,'John'); mysql> insert into DistinctDemo1 values(4,'Carol'); mysql> insert into DistinctDemo1 values(5,'David');
mysql> select *FROM DistinctDemo1;
以下はすべてのレコードを表示する出力です。
+------+-------+ | id | name | +------+-------+ | 1 | John | | 2 | John | | 3 | John | | 4 | Carol | | 5 | David | +------+-------+ 5 セット内の行(0.00 sec)
以下はカウントを取得する语法です。
mysql> SELECT name, COUNT(1) AS 出現値 FROM DistinctDemo1 GROUP BY name ORDER BY 出現値;
これは出力です。
+-------+----------------+ | name | 出現値 | +-------+----------------+ | Carol | 1 | | David | 1 | | John | 3 | +-------+----------------+ 3 セット内の行(0.04 sec)