먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable -> ( -> Name varchar(100), -> Code varchar(100) -> ); Query OK, 0 rows affected (0.54 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
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)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable;
출력
+--------+------+ | Name | Code | +--------+------+ | Chris | 0001 | | Robert | 0002 | | Robert | 0003 | | Chris | 0001 | +--------+------+ 4 rows in set (0.00 sec)
다음은 고유 값의 수를 계산하는 쿼리입니다 -
mysql> select Name,count(distinct Code) from DemoTable group by Name;
출력
+--------+----------------------+ | Name | count(distinct Code) | +--------+----------------------+ | Chris | 1 | | Robert | 2 | +--------+----------------------+ 2 rows in set (0.06 sec)