먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Subject varchar(20), Price int ); Query OK, 0 rows affected (0.64 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable(Subject,Price) values('MySQL',456); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Subject,Price) values('MySQL',456); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Subject,Price) values('MongoDB',56); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Subject,Price) values('MongoDB',60); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Subject,Price) values('Java',100); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Subject,Price) values('Java',130); Query OK, 1 row affected (0.15 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable;
출력
+----+---------+-------+ | Id | Subject | Price | +----+---------+-------+ | 1 | MySQL | 456 | | 2 | MySQL | 456 | | 3 | MongoDB | 56 | | 4 | MongoDB | 60 | | 5 | Java | 100 | | 6 | Java | 130 | +----+---------+-------+ 6 rows in set (0.00 sec)
다음은 MySQL에서 GROUB BY를 사용하여 행을 가져오는 쿼리입니다.
mysql> select sum(Price) AS Total, Subject from DemoTable group by Subject;
출력
+-------+---------+ | Total | Subject | +-------+---------+ | 912 | MySQL | | 116 | MongoDB | | 230 | Java | +-------+---------+ 3 rows in set (0.04 sec)