题解 | #网易云音乐推荐(网易校招笔试真题)#
网易云音乐推荐(网易校招笔试真题)
https://www.nowcoder.com/practice/048ed413ac0e4cf4a774b906fc87e0e7
这题有个天坑就是可能会出现music_likes表中没有user_id=1的行的情况,所以不可以用where music_id != (select music_id from music_likes where user_id = 1)
不然结果就会不报错然后返回0行。
不然结果就会不报错然后返回0行。
最后想了个表连接的方式去过滤掉该用户已经喜欢的music_id,代码如下,看不懂就多看几遍自己分析,至少要自己能看懂答案,不然就浪费了这道题了。
select music_name from
(select distinct t1.music_id music_id from
(select user_id,music_id from music_likes
where user_id in
(select follower_id from follow
where user_id = 1) or
user_id = 1) t1
left join
(select user_id,music_id from music_likes
where user_id = 1) t2
on t1.music_id = t2.music_id
where t2.music_id is null) t3
left join music on t3.music_id = music.id
order by music.id