题解 | #网易云音乐推荐(网易校招笔试真题)#
网易云音乐推荐(网易校招笔试真题)
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在线编程重点试题解析


查看15道真题和解析