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

SELECT music_name
FROM (
    SELECT DISTINCT m.music_name, m.id
    FROM music_likes l
    LEFT JOIN music m ON m.id = l.music_id
    WHERE l.user_id IN (
        SELECT follower_id
        FROM follow
        WHERE user_id = 1
    )
    AND l.music_id NOT IN (
        SELECT DISTINCT music_id
        FROM music_likes
        WHERE user_id = 1
    )
) AS subquery
ORDER BY id;

!:按照id排序,但是最后结果不显示id:

再在外面套一个子查询查名字就好了

坑:

1、DISTINCTORDER BY 不兼容,因为 m.id 没有出现在 SELECT 列表中,却在 ORDER BY 子句中使用。这是 MySQL 的限制:如果使用 DISTINCTORDER BY 中的列必须也出现在 SELECT 列表中

代码:

select distinct m. music_name

from music_likes l

left join music m on m.id=l.music_id

where user_id in(

    select follower_id

    from follow

    where user_id=1

)

and music_id not in (

    select distinct music_id

    from music_likes

    where music_likes.user_id=1

)

ORDER BY m.id;

全部评论

相关推荐

点赞 评论 收藏
分享
2025-12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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