상세 컨텐츠

본문 제목

group by 사용법

My SQL

by yjh0922 2022. 5. 16. 17:42

본문

~~별로, 의 의미를 가지고 있을떄는 group by함수를 사용한다.

이는, 내가 원하는 데이트를 가져올떄 해당 컬럼을 기준으로해서 가져오겠다는 의미입니다.

-- author_lname 별로, 몇권의 책을 썻는지,
-- author_lname과 cnt 라는 컬럼으로 데이터를 가져오기
select author_lname, count(*) as cnt
from books
group by author_lname;

-- full_name 별로, 각각 몇권의 책을 썻는지
-- full_name 과 책의 갯수를 cnt라는 컬럼으로 가져오세요.
select count(*) as cnt, concat( author_fname,' ' ,author_lname) as full_name
from books
group by full_name;

select author_fname,author_lname ,count(*) 
from books
group by author_fname,author_lname;

group by는 컬럼 여러개 별로의 리스트로 묶지 않고 사용할수있다.

관련글 더보기