第一步:用户1关注了哪些人 select follower_id from follow where user_id=1 第二步:用户1喜欢的音乐 select music_id from music_likes where user_id=1 第三步:关注的那些人喜欢的音乐 select music_id from music_likes where user_id in ( select follower_id from follow where user_id=1 ) 第四步:除了用户1喜欢的音乐外,关注的那些人喜欢的其他音乐 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 ) 第五步:与音乐表联结 select distinct m.music_name from music m join ( 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 ) )a on a.music_id=m.id order by m.id
select music_name from music left join music_likes on music.id = music_likes.music_id left join follow on music_likes.user_id =follower_id where follow.user_id = 1 and music_name not in (select music_name from music left join music_likes on music.id = music_likes.music_id left join follow on music_likes.user_id =follow.user_id where follow.user_id = 1) group by music_id order by music_id这道题的难点在于distinct与order by 子句一起运行会报错,可以使用group by 子句代替distinct解决.
select 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 ) order by id
select music_name from music where id IN (select distinct 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) order by id;利用子查询,但需要注意null的处理,最好使用NOT IN剔除自己喜欢的音乐,而不是<>。当自己没有喜欢的音乐时候,使用不等于进行剔除就可能出现问题。
select distinct t3.music_name from follow t1, music_likes t2, music t3 where t1.follower_id = t2.user_id and t2.music_id = t3.id and t1.user_id =1 and t2.music_id not in (select music_id from music_likes where user_id =1) order by t3.id
先将三表按照顺序左连接,找到用户感兴趣的音乐;然后再左连接用户已喜爱的音乐,则用户可能感兴趣的、未喜爱的音乐会在右表出现空值,筛选出有空值的行,对音乐进行去重即可得到最终结果。
select music_name
from (
select distinct m.id, m.music_name
from follow f
left join music_likes ml on f.follower_id=ml.user_id
left join music m on ml.music_id=m.id
left join music_likes ml2 on f.user_id=ml2.user_id and ml.music_id=ml2.music_id
where f.user_id=1 and ml2.user_id is null
order by m.id asc
) x;
with a1 as( select music_id from follow join music_likes on follower_id = music_likes.user_id where follow.user_id = 1 ), #关注人喜欢音乐的ID a2 as( select music_id from music_likes where user_id=1 ) #自己喜欢的音乐ID select music_name from music where id in (select * from a1) and id not in (select * from a2)
select music_name from (select distinct c.id,c.music_name from follow a join music_likes b on a.follower_id=b.user_id 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) ) x order by id
with res as ( select music_id as 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 ) ) select music_name FROM music where id IN(select * from res);
SELECT DISTINCT m.music_name FROM music m,music_likes ml WHERE m.id = ml.music_id AND ml.user_id IN (SELECT follower_id FROM follow WHERE user_id = 1) AND m.id NOT IN (SELECT music_id FROM music_likes WHERE user_id = 1) ORDER BY m.id
with t as ( select d.user_id,d.music_id from music_likes d inner join music c on d.music_id=c.id ) select distinct music_name from (select a.user_id, c.music_name,c.id from follow a inner join music_likes b on a.follower_id=b.user_id inner join music c on b.music_id=c.id where a.user_id=1) a where a.id not in (select t.music_id from t where t.user_id=a.user_id) order by a.id
SELECT music_name FROM ( SELECT DISTINCT M.id, music_name FROM music_likes ML LEFT JOIN music M ON ML.music_id = M.id WHERE ML.user_id IN( SELECT follower_id FROM follow WHERE user_id = 1 ) AND music_name NOT IN( SELECT music_name FROM music_likes ML2 LEFT JOIN music M2 ON ML2.music_id = M2.ID WHERE ML2.user_id = 1 ) ORDER BY M.id ) B
# 连表后字段为:用户id,用户喜欢的音乐,用户关注的人,关注喜欢的音乐,关注喜欢的音乐名 with temp as( select f.user_id user_id ,ml1.music_id user_music ,f.follower_id follower ,ml2.music_id follower_music ,m.music_name music_name from follow f join music_likes ml1 on f.user_id=ml1.user_id join music_likes ml2 on f.follower_id=ml2.user_id join music m on m.id=ml2.music_id ) # 判断条件:用户音乐!=关注音乐,用户1,排序 select music_name from (select distinct music_name,follower_music from temp where user_music != follower_music and user_id=1 order by follower_music) a
select music_name from music_likes left join music on music_likes.music_id = music.id 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_name order by any_value(music_likes.music_id)
select distinct m.music_name from follow f left join music_likes ml on f.follower_id=ml.user_id left join music m on ml.music_id=m.id where m.music_name not in ( select music_name from follow join music_likes on follow.user_id=music_likes.user_id join music on music_likes.music_id=music.id where follow.user_id=1 ) and f.user_id=1 order by m.id