MySQL

MySQL) Instargram (3) : 프론트엔드 개발자에게 보낼 데이터 만들기

567Rabbit 2024. 5. 20. 09:53

 

1) 찾아야 할 데이터 : 이미지 주소, 작성자 이름, 좋아요 수, 태그 이름들

 

(예시 사진 입니다)

 

 

-- 특정 포스팅, photos의 id가 72번이라면 이미지 주소, 작성자 이름, 좋아요 수

select p.id, p.image_url, username , count(l.photo_id) as like_count
from photos p
join users u
on p.user_id = u.id
join likes l
on p.id = l.photo_id
where p.id = 72;

 


-- 특정 포스팅, photos의 id가 72번 이라면 그에 대한 태그들

select photo_id, tag_name
from photos p
join photo_tags pt
on p.id = pt.photo_id
join tags t
on pt.tag_id = t.id
where p.id = 72;

 

 

 

2) 찾아야 할 데이터 : 댓글(코멘트)

 

(예시 사진 입니다)

 

-- 특정 포스팅, photos의 id가 72번이라면 그에 대한 코멘트(댓글)

select photo_id, comment_text
from photos p
join comments c
on p.id = c.photo_id
where p.id = 72;