인스타그램에 기능들 API로 만들어보고 있습니다.


어떤 유저의 팔로워, 팔로잉 리스트를 가져오고

그 리스트 안에 있는 유저 중 나를 팔로우하는 사람이 있으면,

별도 플래그를 넣어서 반환하려고 합니다.



create table member (

id bigint auto_increment primary key,

nickname varchar(60) not null

);


create_table follow(

follower bigint not null, -- 팔로워

followee bigint not null, -- 팔로우 대상


foreign key(`follower`) references `member`(id),

foreign key(`followee`) references `member` (id),

primary key(`follower`, `followee`)

);


이런 테이블이라고 가정했을때,


제 member.id 가  1이라고 하고, member.id 10 에 해당하는 유저의 

팔로워 목록 가져오기 기능을 쿼리로 나타낸다고하면


se1ect 

A.follower as follower,

if (A.follower in (se1ect B.follower from follow B where B.followee = 1  )) as is_my_follower

from follower A

where A.followee=10;


이렇게 짜니까 원하는대로 동작은 하더라고요.

다만 subquery 를 쓰면 문제가 많다고 해서 이걸 self join 형태로 바꿀수 있는지

궁금합니다.