이를 위해 CASE WHEN 개념을 사용합니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table demo68 −> ( −> id int not null auto_increment primary key, −> company_name varchar(50), −> employee_name varchar(50), −> country_name varchar(50) −> ); Query OK, 0 rows affected (1.86 sec)
insert 명령을 사용하여 일부 레코드를 테이블에 삽입하십시오 -
mysql> insert into demo68(company_name,employee_name,country_name) values('Google','John','US'); Query OK, 1 row affected (0.29 sec) mysql> insert into demo68(company_name,employee_name,country_name) values('Google','Bob','UK'); Query OK, 1 row affected (0.10 sec) mysql> insert into demo68(company_name,employee_name,country_name) values('Google','David','AUS'); Query OK, 1 row affected (0.08 sec)
select 문을 사용하여 테이블의 레코드 표시 -
mysql> select *from demo68;
이것은 다음과 같은 출력을 생성합니다 -
+----+--------------+---------------+--------------+ | id | company_name | employee_name | country_name | +----+--------------+---------------+--------------+ | 1 | Google | John | US | | 2 | Google | Bob | UK | | 3 | Google | David | AUS | +----+--------------+---------------+--------------+ 3 rows in set (0.00 sec)
다음은 MySQL에서 몇 개의 행 레코드를 결합하는 쿼리입니다 -
mysql> select −> company_name, −> max(case when country_name= 'US' then employee_name end) as US_Employee_Name, −> max(case when country_name= 'UK' then employee_name end) as UK_Employee_Name, −> max(case when country_name= 'AUS' then employee_name end) as AUS_Employee_Name −> from demo68 −> group by company_name;
이것은 다음과 같은 출력을 생성합니다 -
+--------------+------------------+------------------+-------------------+ | company_name | US_Employee_Name | UK_Employee_Name | AUS_Employee_Name | +--------------+------------------+------------------+-------------------+ | Google | John | Bob | David | +--------------+------------------+------------------+-------------------+ 1 row in set (0.05 sec)