/*
select follower_id
from follow
where user_id = 1;

select music_id
from music_likes as ml
where user_id = 1;

select music_id
from music_likes as ml
where user_id in (select follower_id from follow where user_id = 1)
and music_id not in (select music_id from music_likes where user_id = 1);
*/
select music_name
from music
where id in (
    select music_id
    from music_likes as ml
    where user_id in (select follower_id from follow where user_id = 1)
    and music_id not in (select music_id from music_likes where user_id = 1)
)
order by id;

我相信,这道题一定有其他看似简单的解答方法。但是,这道题我还是倾向于运用到经典的剥洋葱思路。

  • 第一步:寻找user_id=1的关注者;
  • 第二步:寻找user_id=1喜欢的music_id
  • 第三步:检索user_id=1的关注者喜欢的music_id,并排除第二步中的检索结果;
  • 第四步:根据id检索music_name,并排序。