Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(100), -> Code varchar(100) -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','0001'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Robert','0002'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Robert','0003'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris','0001'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------+------+ | Name | Code | +--------+------+ | Chris | 0001 | | Robert | 0002 | | Robert | 0003 | | Chris | 0001 | +--------+------+ 4 rows in set (0.00 sec)
Following is the query to count the number of distinct values −
mysql> select Name,count(distinct Code) from DemoTable group by Name;
Output
+--------+----------------------+ | Name | count(distinct Code) | +--------+----------------------+ | Chris | 1 | | Robert | 2 | +--------+----------------------+ 2 rows in set (0.06 sec)