union all
테이블과 테이블을 붙일 때 해당 명령어가 필요하다 기본 골격은 아래와 같이 작성하면 된다.
테이블에서 추출하고자 하는 정보를 뽑아 낸 뒤에 결과물과 결과물을 union all로 서로
붙여서 작성하면 결과값을 연달아 나열할 수 있는 것이다. 예를 들어 결과물이 A,B,C 인
테이블이 있다고 했을 때 union all로 붙이면 'ABC' 라는 결과물을 얻을 수 있게 된다.
(
query 문
)
union all
(
query 문
)
.
.
.
(
query 문
)
union all 예시
(
select '7월' as month, c.title, c2.week, count(*) as cnt from checkins c2
inner join courses c on c2.course_id = c.course_id
inner join orders o on o.user_id = c2.user_id
where o.created_at < '2020-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
)
union all
(
select '8월' as month, c.title, c2.week, count(*) as cnt from checkins c2
inner join courses c on c2.course_id = c.course_id
inner join orders o on o.user_id = c2.user_id
where o.created_at >= '2020-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
)
결과
결과물을 보면 7월 달 부분과 8월달 부분 테이블을 붙여서 결과물을 표시한 것을 확인할 수 있다.