MySQL - SUM

임재현·2021년 5월 6일
0

MySQL

목록 보기
32/52
post-custom-banner

Udemy - The Ultimate MySQL Bootcamp: Go from SQL Beginner to Expert를 수강하며 정리하는 글

SUM

제일 기본 사용법 :

SELECT SUM(칼럼명) 
FROM 테이블명;
mysql> SELECT SUM(pages) FROM books;
+------------+
| SUM(pages) |
+------------+
|       6158 |
+------------+
1 row in set (0.00 sec)

GROUP BY와 함께 사용

  • Q. Sum all pages each author has written

mysql> SELECT author_fname, author_lname, SUM(pages) 
	FROM books GROUP BY author_fname, author_lname;
+--------------+----------------+------------+
| author_fname | author_lname   | SUM(pages) |
+--------------+----------------+------------+
| Jhumpa       | Lahiri         |        489 |
| Neil         | Gaiman         |        512 |
| Dave         | Eggers         |       1293 |
| Michael      | Chabon         |        634 |
| Patti        | Smith          |        304 |
| Raymond      | Carver         |        702 |
| Don          | DeLillo        |        320 |
| John         | Steinbeck      |        181 |
| David        | Foster Wallace |        672 |
| Dan          | Harris         |        256 |
| Freida       | Harris         |        428 |
| George       | Saunders       |        367 |
+--------------+----------------+------------+
12 rows in set (0.00 sec)
profile
임재현입니다.
post-custom-banner

0개의 댓글