이 글은 전 포스팅에 이어지는 글입니다.
https://jminie.tistory.com/114
이번 글에는 유튜브의 각 화면을 SQL문 하나로 가져오는 이른바 한방 쿼리(?)를 사용하여 화면에 맞는 데이터를 불러와 보도록 하겠다.
모든 부분을 일일이 다 가져오지는 못했다. 당초 유튜브라는 거대한 앱의 모든 데이터들을 ERD화 시키기가 힘들었다.
📌 한방 쿼리
1. 유튜브 첫 화면
select V.thumbNail, V.vidioName, V.createdAt, V.vidio, C.channelName ,C.channelProfileImage
from YouTube.Vidio as V inner join YouTube.Channel as C
on V.channelId = C.channelId
WHERE C.status = '활성' and V.status = '활성';
Vidio테이블과 Channel테이블을 이너 조인시켜서 그중 영상과 채널이 모두 정지가 아닌 즉 '활성' 상태인 비디오의
썸네일, 비디오이름, 비디오가 생성된 시기, 채널 이름 그리고 채널 프로필 사진을 불러왔다.
2. 계정 화면
select U.userNickname, U.userEmail, YP.status, U.userProfileImage, U.subscriber
from YouTube.User as U inner join YouTube.YouTubePrimium as YP
on U.userId = YP.userId
where U.userId = 2;
User 테이블과 YouTubePrimium 테이블을 이너 조인시켜서 그중 userId값이 2인 (user가 '정현우'인) 값 중에서
유저 이름, 유저 이메일, 프리미엄 여부, 프로필 사진, 구독자 수를 불러왔다.
3. 채널 커뮤니티 글 화면
select C.channelName, YC.createdAt, YC.communityMainText
from YouTube.Channel as C inner join YouTube.Community as YC
on C.channelId = YC.channelId
where C.channelId = 1 and YC.communityId = 1;
Channel테이블과 Communtiy테이블을 이너 조인시켜서 채널 id가 1이고(채널이 '개발바닥'이고) community id가 1인(커뮤니티 글 중 첫 번째 글)인 조건을 주어 그중 채널 이름 생성일자 커뮤니티 본문 글을 불러왔다.
4. (기능 추가) 채널 커뮤니티 글 화면 + 좋아요 수 댓글 수
select channel.channelProfileImage, channel.channelName,community.createdAt, community.communityMainText, CClike.`좋아요 개수`,CCComent.`댓글 개수`
from YouTubeUp.Channel as channel
inner join YouTubeUp.Community as community
on channel.channelId = community.channelId
inner join (select count(CLike.communityLikeId) as '좋아요 개수'
from YouTubeUp.Community as commuinty
inner join YouTubeUp.CommunityLike as CLike
on commuinty.communityId = CLike.communityId
where CLike.communityId = 1
group by CLike.communityId) CClike
inner join (select count(comment.CCommentId) as '댓글 개수'
from YouTubeUp.Community as community
inner join YouTubeUp.CommunityComment as comment
on community.communityId = comment.communityId
where comment.communityId = 1
group by comment.communityId) CCComent
where community.communityId = 1;
커뮤니티글 화면에서 좋아요 수와 댓글 개수까지 가져오는 한방 쿼리문이다.
우선 채널과 커뮤니티를 조인시킨 뒤 좋아요 수와 댓글 수를 가져오는 쿼리문을 각각 작성 후 서브 쿼리(쿼리문 안에 쿼리문이 있는 형태)로 각각 이너 조인시켜주어 count함수로 좋아요 개수와 댓글 개수를 조건이 commnityId가 1인 글에서 가져왔다.
5. 영상 시청 시 화면
select vidio.vidio, vidio.vidioName, vidio.createdAt as '생성시기', channel.channelProfileImage,
channel.channelName, CS.구독자수, VVC.댓글수, VVVC.vidioComment
from YouTubeUp.Channel as channel
inner join YouTubeUp.Vidio as vidio
on channel.channelId = vidio.channelId
inner join (select count(Vcoment.vidioCommentId) as '댓글수', vidio.channelId
from YouTubeUp.Vidio as vidio
inner join YouTubeUp.VidioComment as Vcoment
on vidio.vidioId = Vcoment.vidioId
where vidio.vidioId = 1
group by vidio.vidioId) VVC on channel.channelId = VVC.channelId
inner join (select count(user.userId) as '구독자수', subscribe.channelId
from YouTubeUp.Channel as channel
inner join YouTubeUp.Subscribe as subscribe
on channel.channelId = subscribe.channelId
inner join YouTubeUp.User as user
on subscribe.userId = user.userId
where channel.channelId = 1
group by subscribe.channelId) CS on channel.channelId = CS.channelId
inner join (select comment.vidioComment, vidio.channelId
from YouTubeUp.Vidio as vidio
inner join YouTubeUp.VidioComment as comment
on vidio.vidioId = comment.vidioId
where comment.vidioCommentId = 1) as VVVC on channel.channelId = VVVC.channelId
where channel.channelId = 1 and vidio.vidioId = 1;
이너 조인을 4번이나 쓴 한방 쿼리이다.
우선 원하는 각 정보를 보여주는 select문을 4개 작성한 후 모두 서브 쿼리로 이어 붙였다.
이때 내가 중요하게 생각한 것은 이너 조인을 시킬 때 모두 공통된 값 즉 channelId 값으로 이어줬다는 것이다.
구독자수와 댓글 수는 각각 subscribe에 있는 userId와 vidiocomment에 있는 vidiocommnetId 값을 count함수로 세어서 나타내었다.
(각 구독자 테이블과 댓글 테이블 id값은 각각 모두 고유하므로 이 방법이 중복되지 않고 구독자수와 댓글 수를 세는 가장 좋은 방법이라 생각했다.)
'YouTube Clone Project' 카테고리의 다른 글
YouTube 클론 프로젝트 - OAuth를 이용한 카카오 로그인 구현 (Spring Boot) (0) | 2021.10.29 |
---|---|
YouTube 클론 프로젝트 - JWT를 이용한 로그인 인증처리 (Spring Boot) (0) | 2021.10.18 |
YouTube 클론 프로젝트 - API 리스트업 및 기능 구현 (Spring Boot) (1) | 2021.10.11 |
YouTube 클론 프로젝트 - Product App(YouTube) 데이터 모델링 및 ERD 구축 (0) | 2021.10.02 |