首页 > 试题广场 >

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

[编程题]网易云音乐推荐(网易校招笔试真题)
  • 热度指数:128174 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
假设云音乐数据库里面现在有几张如下简化的数据表:
关注follow表,第一列是关注人的id,第二列是被关注人的id,这2列的id组成主键
user_id follower_id
1 2
1 4
2 3
比如第一行代表着用户id为1的关注着id为2的用户

个人的喜欢的音乐music_likes表,第一列是用户id,第二列是喜欢的音乐id,这2列的id组成主键
user_id music_id
1 17
2 18
2 19
3 20
4 17
比如第一行代表着用户id为1的喜欢music_id为17的音乐

音乐music表,第一列是音乐id,第二列是音乐name,id是主键
id music_name
17 yueyawang
18 kong
19 MOM
20 Sold Out

请你编写一个SQL,查询向user_id = 1 的用户,推荐其关注的人喜欢的音乐。
不要推荐该用户已经喜欢的音乐,并且按music的id升序排列。你返回的结果中不应当包含重复项
上面的查询结果如下:
music_name
kong
MOM
示例1

输入

CREATE TABLE `follow` (
`user_id` int(4) NOT NULL,
`follower_id` int(4) NOT NULL,
PRIMARY KEY (`user_id`,`follower_id`));

CREATE TABLE `music_likes` (
`user_id` int(4) NOT NULL,
`music_id` int(4) NOT NULL,
PRIMARY KEY (`user_id`,`music_id`));

CREATE TABLE `music` (
`id` int(4) NOT NULL,
`music_name` varchar(32) NOT NULL,
PRIMARY KEY (`id`));

INSERT INTO follow VALUES(1,2);
INSERT INTO follow VALUES(1,4);
INSERT INTO follow VALUES(2,3);

INSERT INTO music_likes VALUES(1,17);
INSERT INTO music_likes VALUES(2,18);
INSERT INTO music_likes VALUES(2,19);
INSERT INTO music_likes VALUES(3,20);
INSERT INTO music_likes VALUES(4,17);

INSERT INTO music VALUES(17,'yueyawang');
INSERT INTO music VALUES(18,'kong');
INSERT INTO music VALUES(19,'MOM');
INSERT INTO music VALUES(20,'Sold Out');

输出

kong
MOM
用了直接方法,建了两个临时表,拿到了 user1的喜欢人和喜欢歌曲,之后剔除就行
with t1 as(
    select
        follower_id
    from
        follow
    where user_id = 1),
t2 as(
    select 
        (case when music_id in (select music_id from music_likes where user_id=1) then null else music_id end) as name
    from 
        music_likes
    where user_id in (select follower_id from t1)
)
select music_name from music where id in (select name from t2)


发表于 2025-05-08 15:57:11 回复(0)
select music_name
from follow a 
left join music_likes b 
on a.follower_id = b.user_id 
left join music c 
on b.music_id = c.id
where a.user_id = 1
and b.music_id not in (
    select music_id 
    from music_likes 
    where user_id = 1)
group by c.id
order by c.id

发表于 2025-05-07 15:03:45 回复(0)
with tmp as
(
    select
    user_id,
    music_id,
    music_name
    from music_likes
    left join music
    on music.id=music_likes.music_id
)
select
music_name
from
(
    select
    distinct music_name,
    music_id
    from follow
    inner join tmp
    on follow.follower_id=tmp.user_id
    where follow.user_id=1
    and music_name not in
    (
        select
        distinct music_name
        from follow
        inner join tmp
        on follow.user_id=tmp.user_id
        where follow.user_id=1
    )
) as t
order by music_id
发表于 2025-04-29 17:44:14 回复(0)
select music_name from (select distinct m.music_name, ml.music_id from follow f join music_likes ml on f.follower_id = ml.user_id join music m on ml.music_id = m.id where f.user_id = 1
) a where (select music_id from music_likes where user_id = 1 ) not in (a.music_id) order by a.music_id
发表于 2025-04-17 17:56:34 回复(0)
select music_name
from(
select distinct  c.music_name, b.music_id  
from follow a
left join music_likes b
on a.follower_id=b.user_id
left join music c
on b.music_id=c.id
where a.user_id=1 and b.music_id not in(select music_id from music_likes where user_id =1)
order by b.music_id
)d
发表于 2025-04-09 13:54:08 回复(0)
with new1 as(
    select
    distinct f.user_id,music_id,music_name
    from
    follow f
    join
    music_likes ml
    on f.follower_id=ml.user_id
    join
    music m
    on ml.music_id=m.id
    where f.user_id=1
)
    select
    n1.music_name
    from
    new1 n1
    join
    music_likes ml
    on n1.user_id=ml.user_id
    where n1.music_id <> ml.music_id
    order by n1.music_id
求助:为什么这个代码只能通过80%
发表于 2025-04-07 14:46:18 回复(0)
select
 music_name
from (
 select music_id from music_likes
 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)
 group by music_id
) a
join music m on a.music_id = m.id
order by id


发表于 2025-04-03 17:59:26 回复(0)
三表联表然后在where条件中筛选出id=1的用户,并筛选掉该用户喜欢的音乐。遇到的问题就是本题中distinct和order by不能同时用,我想到的办法是套个子查询查,最后选择了用group by做歌曲的去重,这样sql语句会简洁一点
select	music_name
from	follow f
join	music_likes m1 on f.follower_id = m1.user_id
join	music m2 on m1.music_id = m2.id
where f.user_id = 1 and music_id not in (
		select music_id
		from	music_likes
		where user_id = 1
)
group by music_name,m1.music_id
order by m1.music_id asc

发表于 2025-03-31 21:50:38 回复(0)
在使用剔除时不要使用 <>,而是用 not in,因为空值与任何值比较都是未知的,如果存在null值,结果会过滤掉所有的结果;id_mus 的查询结果,保证了not in () 中的结果是非空的,否则也会出现  无结果返回 的情况
with f_mus as(
    select mc.id,mc.music_name
    from follow fw 
    left join music_likes ms on fw.follower_id = ms.user_id
    left join music mc on ms.music_id = mc.id 
    where fw.user_id = 1

),
id_mus as(
    select mc.id,mc.music_name
    from music_likes ms 
    left join music mc on ms.music_id = mc.id
    where ms.user_id = 1
)
select music_name
from(
    select distinct id,music_name
    from f_mus
    where  id not in(select id from id_mus)
    order by id
) as fin_mn

发表于 2025-03-31 21:22:58 回复(0)
两种解法:::
解法一用join:
select music.music_name
from(
    select distinct music_id
    from
    (
        select *
        from follow
        where user_id=1
    ) t1
    join music_likes ml
    on t1.follower_id=ml.user_id
    where music_id not in (select music_id from music_likes where user_id=1)
) t2
join music
on t2.music_id=music.id
order by music.id

注意select distinct music_id这里只挑music_id出来就行了,调试的时候select distinct user_id, music_id   结果怎么都跑不出来

解法2用in:
select music_name
from music
where music.id in
(
    select distinct music_id
    from
    (
        select distinct ml.user_id, music_id
        from
        (
            select *
            from follow
            where user_id=1
        ) t1
        join music_likes ml
        on t1.follower_id=ml.user_id
        where music_id not in (select music_id from music_likes where user_id=1)
    ) t2
)
order by music.id







发表于 2025-03-30 16:07:48 回复(0)
with t1 as(
select
a.user_id,
a.music_id
from music_likes a
where a.user_id =1
),
t2 as (
select
c.follower_id AS friend_id,
d.music_id,
m.music_name
FROM follow c
JOIN music_likes d ON c.follower_id = d.user_id
JOIN music m ON d.music_id = m.id
WHERE c.user_id = 1
)
select
t2.music_name
from t2
where
t2.music_id not in (select t1.music_id from t1 )
GROUP BY t2.music_id, t2.music_name
order by t2.music_id;

发表于 2025-03-25 10:47:42 回复(0)
select music_name
from music
where id in(select distinct music_id from follow inner join music_likes on follow.follower_id=music_likes.user_id and follow.user_id = 1)
and id not in(select music_id from music_likes  where user_id = 1)
order by id
原来也没有很复杂,妈的,困了我一个小时
发表于 2025-03-18 20:35:37 回复(0)
要输出歌曲名字,下意识的用歌曲名来作为筛选条件,但结果总是8/10。
问了ai原来是会出现重名的情况,得用歌曲id作为筛选条件才行。
with a as (
select distinct music_name,music_id
from follow fo
join music_likes ml on fo.follower_id = ml.user_id
join music m on music_id = id
where fo.user_id = '1')
,b as(
    select music_id
    from music_likes 
where user_id = '1'
)
select a.music_name
from a
where a.music_id not in (select b.music_id from b)
order by music_id


发表于 2025-03-16 14:34:30 回复(1)
select music_name as name
from (
    select distinct t2.music_name, t2.id as music_id
    from (
        select
            a.user_id,
            a.follower_id,
            b.music_id as mic_1,
            c.music_id as mic_2
        from follow a
        left join music_likes b on a.follower_id = b.user_id
        left join music_likes c on a.user_id = c.user_id
        where b.music_id <> c.music_id
          and a.user_id = '1'
    ) t1
    left join music t2 on t1.mic_1 = t2.id
    order by t2.id
) t3;
#这个自测可以通过,为什么提交不行,还不能进行比对

发表于 2025-02-20 11:12:33 回复(0)
select distinct music_name
from music
where id in (
    select music_id from music_likes
    where user_id in (select follower_id from follow where user_id = 1)
) and id not in (select music_id from music_likes where user_id = 1);

发表于 2025-02-19 16:48:03 回复(0)
WITH
    t1 AS (
-- 用户喜欢的音乐
        SELECT
            user_id,
            music_id,
            music_name
        FROM
            music_likes ml
            LEFT JOIN music m ON ml.music_id = m.id
    ),
    t2 AS (
        SELECT DISTINCT
            music_id,
            music_name
        FROM
            t1
        WHERE
            user_id IN (
-- 用户关注的用户id
                SELECT
                    follower_id
                FROM
                    follow
                WHERE
                    user_id = 1
            )
            AND music_name NOT IN (
 -- 用户1喜欢的音乐
                SELECT
                    music_name
                FROM
                    t1
                WHERE
                    user_id = 1
                    AND music_name IS NOT NULL
            )
    )
SELECT
    music_name
FROM
    t2
ORDER BY
    music_id;

发表于 2025-02-13 12:14:44 回复(0)
# id1喜欢的music
with r1 as (select t2.music_name
            from music_likes t1,
                 music t2
            where t1.user_id = 1
              and t1.music_id = t2.id),
# 被关注喜欢的music
     r2 as (select distinct t3.music_name, t3.id
            from follow t1
                     join music_likes t2 on t1.user_id = 1 and t1.follower_id = t2.user_id
                     join music t3 on t2.music_id = t3.id)
select r2.music_name
from r2
         left join r1 on r1.music_name = r2.music_name
where r1.music_name is null
order by r2.id;


发表于 2025-01-30 17:41:15 回复(0)
with u1_follow_music_name as (
select
distinct music_id,
music_name
from music_likes t1
join music t2 on t1.music_id = t2.id
where t1.user_id in (
select
follower_id
from follow
where user_id = 1
)
)

select
music_name
from u1_follow_music_name
where music_id not in (
select
music_id
from music_likes
where user_id = 1
)
order by music_id
发表于 2025-01-20 16:37:02 回复(0)