English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Ecco un esempio di come ottenere il conteggio di ciascun valore diverso in una colonna. Prima di tutto, creeremo una tabella.
Il comando CREATE viene utilizzato per creare tabelle.
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;
Ecco l'output che mostra tutti i record.
+------+-------+ | id | name | +------+-------+ | | 1 | John | | | 2 | John | | | 3 | John | | | 4 | Carol | | | 5 | David | +------+-------+ 5 righe nel set (0.00 sec)
Ecco la sintassi per ottenere il conteggio.
mysql> SELECT name, COUNT(1) as OccurenceValue FROM DistinctDemo1 GROUP BY name ORDER BY OccurenceValue;
Questo è l'output.
+-------+----------------+ | name | OccurenceValue | +-------+----------------+ | Carol | | 1 | | David | | 1 | | John | | 3 | +-------+----------------+ 3 righe nel set (0.04 sec)