이를 위해 CASE 문을 사용합니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable -> ( -> LastName varchar(100) -> ); Query OK, 0 rows affected (0.60 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Taylor'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Taylor'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Miller'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Miller'); Query OK, 1 row affected (0.12 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable;
출력
이것은 다음과 같은 출력을 생성합니다 -
+----------+ | LastName | +----------+ | Smith | | Brown | | Brown | | Smith | | Taylor | | Taylor | | Miller | | Miller | | Miller | +----------+ 9 rows in set (0.00 sec)
다음은 하나의 쿼리에서 여러 COUNT() 메서드를 구현하는 쿼리입니다. -
mysql> select COUNT(CASE WHEN LastName='Smith' THEN 1 END) AS TotalFrequency, -> COUNT(CASE WHEN LastName='Brown' THEN 1 END) AS TotalFrequency, -> COUNT(CASE WHEN LastName='Taylor' THEN 1 END) AS TotalFrequency, -> COUNT(CASE WHEN LastName='Miller' THEN 1 END) AS TotalFrequency -> from DemoTable;
출력
이것은 다음과 같은 출력을 생성합니다 -
+----------------+----------------+----------------+----------------+ | TotalFrequency | TotalFrequency | TotalFrequency | TotalFrequency | +----------------+----------------+----------------+----------------+ | 2 | 2 | 2 | 3 | +----------------+----------------+----------------+----------------+ 1 row in set (0.00 sec)