이를 위해 GROUP BY와 함께 COUNT(*)를 사용할 수 있습니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentAge int ); Query OK, 0 rows affected (0.59 sec)
삽입 명령을 사용하여 테이블에 레코드 삽입 -
mysql> insert into DemoTable(StudentAge) values(16); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(StudentAge) values(17); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(StudentAge) values(18); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentAge) values(17); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentAge) values(17); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(StudentAge) values(17); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(StudentAge) values(18); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentAge) values(18); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentAge) values(19); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentAge) values(19); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(StudentAge) values(16); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentAge) values(16); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentAge) values(15); Query OK, 1 row affected (0.16 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable;
이것은 다음과 같은 출력을 생성합니다 -
+-----------+------------+ | StudentId | StudentAge | +-----------+------------+ | 1 | 16 | | 2 | 17 | | 3 | 18 | | 4 | 17 | | 5 | 17 | | 6 | 17 | | 7 | 18 | | 8 | 18 | | 9 | 19 | | 10 | 19 | | 11 | 16 | | 12 | 16 | | 13 | 15 | +-----------+------------+ 13 rows in set (0.00 sec)
다음은 빈도를 계산하는 쿼리입니다 -
mysql> select StudentAge,count(*) AS `AgeFrequency` from DemoTable group by StudentAge;
이것은 다음과 같은 출력을 생성합니다 -
+------------+--------------+ | StudentAge | AgeFrequency | +------------+--------------+ | 16 | 3 | | 17 | 4 | | 18 | 3 | | 19 | 2 | | 15 | 1 | +------------+--------------+ 5 rows in set (0.05 sec)