INNER JOIN ~ ON : 공통된 부분을 JOIN 해주는 것이다.
SELECT COUNT(*) AS 부서별 직원수, ORG FROM employee
GROUP BY ORG
SELECT i.txn_date from store_info s
inner join internet_sales i on s.population = i.txn_date
select * from info i
inner join order o on i.num = o.num
UNION은 무엇인가 ?
필드가 같은 테이블을 이어보고싶을때 붙여주는 것이 유니온이다!
(
select '7월' AS MONTH, c1.title, c2.week, count(*) as cnt from courses c1
inner join checkins c2 on c1.course_id = c2.course_id
inner join orders o on c2.user_id = o.user_id
where o.created_at < '2020-08-01'
group by c1.title, c2.week
)
UNION ALL
(
select '8월' AS MONTH, c1.title, c2.week, count(*) as cnt from courses c1
inner join checkins c2 on c1.course_id = c2.course_id
inner join orders o on c2.user_id = o.user_id
where o.created_at >= '2020-08-01'
group by c1.title, c2.week
)
union에선 order by가 적용되지 않는다!
서브쿼리를 통해서 정렬이 가능하다고한다! 나중에 배워볼 것이라고함
SQL쿼리 안에 또다른 SQL쿼리가 있는 것을 말한다.
예를 들어서 카카오페이로 결제한 유저들의 정보를 보기 위해서
select u.user_id, u.name, u.email from users u
inner join orders o on u.user_id = o.user_id
where o.payment_method = 'kakaopay'
이렇게 조인문을 사용하는데
좀 더 직관적인 서브쿼리문을 사용하면
select u.user_id, u.name, u.email from users u
where u.user_id in (
select user_id from orders
where payment_method = 'kakaopay'
)
이런식으로 가능하다. 순서는 안쪽에 있는 퀴리문이 먼저 실행이 되고나서 바깥으로 나온다.
어디든 들어갈수있고, 복잡할수있지만 원하는 데이터를가져 올수있다!
자주쓰이는 Subquery는 where, select, from 절에서 유용하게 사용될 수 있다!
with table1 as (
select course_id, count(distinct(user_id)) as cnt_checkins from checkins
group by course_id
), table2 as (
select course_id, count(*) as cnt_total from orders
group by course_id
)
select c.title,
a.cnt_checkins,
b.cnt_total,
(a.cnt_checkins/b.cnt_total) as ratio
from table1 a inner join table2 b on a.course_id = b.course_id
inner join courses c on a.course_id = c.course_id
with을 사용해줘서 쿼리하나를 임시 테이블로 만들어준다
실제 업무에서는 문자열 데이터를 원하는 형태로 한번 정리해야하는 경우가 많다!
문자열 쪼개기
SELECT user_id , email, SUBSTRING_INDEX(email,'@',1) FROM users
문자열로 데이터가 들어가있는 email에서 SUBSTRING_INDEX(email,'@',1)를 사용하면
email을 @ 기준을 쪼개는데 1은 앞에있는것을 뜻한다. -1은 뒤에것!
예를들면 123@naver.com 1은 123을 -1은 naver.com을 보여줄 것이다.
select substring(created_at,1,10) as date, count(*) as cnt_date from orders
group by date
인상적이었는데 SUBSTRING을 사용해서 일별날짜로 만들고 날짜별로 묶어서 그날 총 주문 개수를 수를 만들어주는 코드이다.
CASE WHEN 조건~~ THEN 참이면 실행 ELSE 거짓이면 실행
SELECT pu.user_id , pu.point,
(case when pu.point > 10000 then '잘 하고 있어요!'
else '조금만 더 파이팅!' end) as msg
FROM point_users pu
포인트가 10000점이 넘는 사람들에 한해서는 잘하고있다는 메세지가 나오고 그렇지 못하는 사람들에게는 옆에 새로운 필드가 생성되서 메세지가 출력된다.
select pu.point_user_id, pu.point,
case
when pu.point > 10000 then '1만 이상'
when pu.point > 5000 then '5천 이상'
else '5천 미만'
END as lv
from point_users pu
조건을 여러개 줄 수도 있다!
CDN 생성하기
생성한 S3를 바탕으로 CDN을 생성해주었다.
Certificate Manager 인증서 요청
인증서 관리를 쉽게 할 수 있는 서비스이다. (https)
가비아 DNS 레코드에서 호스트와 값/ 위치에 각각 aws 인증서에 해당하는 값을 입력해준다.
인증서 상태에서 발급됨으로 나오는 것을 확인하고,
CloudFront에서 설정해주고 브라우저에서 확인!
크 깔끔하고 알찬 정리