题解 | #网易云音乐推荐(网易校招笔试真题)#

网易云音乐推荐(网易校招笔试真题)

http://www.nowcoder.com/practice/048ed413ac0e4cf4a774b906fc87e0e7

/*
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,并排序。
MySQL试题答案解析 文章被收录于专栏

MySQL在线编程重点试题解析

全部评论

相关推荐

已注销:bro不如吃顿疯狂星期四
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务