Database

[MySQL] GROUP_CONCAT 사용방법

로넌 2022. 9. 19. 14:13

CONCAT

- MySQL에서 지원하는 문자열 함수로, 두 개 이상의 문자열을 결합하여 단일 값으로 반환하는 함수

  즉, 2개 이상의 엔티티를 결합하는 것을 의미한다.

 

예시 )

SELECT CONCAT('APPLE', 'BANANA') FROM FRUIT;

결과 : APPLEBANANA

 

GROUP_CONCAT 사용하는 방법

SELECT '그룹 대상 컬럼', GROUP_CONCAT('CONCAT 대상 컬럼') FROM 테이블명 GROUP BY '그룹 대상 컬럼';

 

예시 1) 

예시 2)

위와 같이 2가지 방법으로 GROUP_CONCAT을 사용할 수 있고, SEPARATOR(구분자)를 사용하여 GROUP_CONCAT 할 수도 있다.  예시 2를 활용하여 데이터를 조회하면 다음과 같은 결과를 얻을 수 있다.

 

 

 

참고

https://dev.mysql.com/doc/refman/5.6/en/aggregate-functions.html#function_group-concat

https://mentha2.tistory.com/182

 

MySQL :: MySQL 5.6 Reference Manual :: 12.19.1 Aggregate Function Descriptions

12.19.1 Aggregate Function Descriptions This section describes aggregate functions that operate on sets of values. They are often used with a GROUP BY clause to group values into subsets. Table 12.23 Aggregate Functions Name Description AVG() Return the

dev.mysql.com