WITH ROLLUP −
를 사용하는 구문은 다음과 같습니다.select yourColumnName1,sum(yourColumnName2) from yourTableName group by yourColumnName1 with rollup;
먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1622 -> ( -> EmployeeCountryName varchar(20), -> EmployeeSalary int -> ); Query OK, 0 rows affected (0.44 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable1622 values('US',4500); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1622 values('UK',1500); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1622 values('AUS',1700); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1622 values('UK',1900); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1622 values('US',3900); Query OK, 1 row affected (0.42 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable1622;
이것은 다음과 같은 출력을 생성합니다 -
+---------------------+----------------+ | EmployeeCountryName | EmployeeSalary | +---------------------+----------------+ | US | 4500 | | UK | 1500 | | AUS | 1700 | | UK | 1900 | | US | 3900 | +---------------------+----------------+ 5 rows in set (0.00 sec)
다음은 MySQL에서 WITH ROLLUP을 올바르게 사용하는 쿼리입니다 -
mysql> select EmployeeCountryName,sum(EmployeeSalary) from DemoTable1622 -> group by EmployeeCountryName with rollup;
이것은 다음과 같은 출력을 생성합니다 -
+---------------------+---------------------+ | EmployeeCountryName | sum(EmployeeSalary) | +---------------------+---------------------+ | AUS | 1700 | | UK | 3400 | | US | 8400 | | NULL | 13500 | +---------------------+---------------------+ 4 rows in set (0.02 sec)