티스토리 뷰

MySQL

[MySQL] 집계 함수

Gayeong's 2022. 4. 14. 19:25

집계 함수

CREATE TABLE myRecode(
    recodeID int(10) unsigned auto_increment,
    memberID int(10) unsigned,
    javascript tinyint NOT NULL,
    html tinyint NOT NULL,
    css tinyint NOT NULL,
    mysql tinyint NOT NULL,
    react tinyint NOT NULL,
    PRIMARY KEY (recodeID)
) charset=utf8;

INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(1, 50, 30, 40, 30, 10);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(2, 40, 10, 20, 10, 20);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(3, 50, 70, 60, 50, 30);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(4, 60, 40, 50, 70, 10);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(5, 70, 60, 40, 30, 20);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(6, 80, 30, 60, 30, 30);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(7, 90, 30, 40, 50, 10);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(8, 10, 90, 40, 10, 10);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(9, 10, 20, 40, 40, 10);
INSERT INTO myRecode(memberID, javascript, html, css, mysql, react) VALUES(10, 30, 10, 90, 70, 60);

집계함수의 종류

SELECT count(javascript) FROM myRecode;
SELECT sum(javascript) FROM myRecode;
SELECT avg(javascript) FROM myRecode;
SELECT max(javascript) FROM myRecode;
SELECT min(javascript) FROM myRecode;

그룹별 집계

GROUP BY

SELECT 필드명 FROM 테이블명 GROUP BY 필드명
SELECT count(javascript) FROM myRecode;
댓글
© 2018 webstoryboy