#第一步:找出用户喜欢的音乐id
#第二步:找出follower喜欢的音乐
#第三步:两表合并(注意联合主键)
#第四步:根据条件筛选出关注者喜欢的音乐,不能和自己喜欢的重复:指定user_id = 1 ,特别注意筛选条件是关注者的音乐不在自己喜欢的音乐列表里(where子句)
#第五步:左连接音乐表即可
SELECT f.user_id, follower_id, ml.music_id "user__likes__music_id" FROM follow f LEFT JOIN music_likes ml ON f.user_id = ml.user_id
#第二步:找出follower喜欢的音乐
SELECT f.user_id, follower_id, ml.music_id "follower__likes__music_id" FROM follow f LEFT JOIN music_likes ml ON f.follower_id = ml.user_id
#第三步:两表合并(注意联合主键)
SELECT t1.user_id, t1.follower_id, IFNULL(user__likes__music_id,0) user__likes__music_id, IFNULL(follower__likes__music_id,0) follower__likes__music_id FROM (SELECT f.user_id, follower_id, ml.music_id "user__likes__music_id" FROM follow f LEFT JOIN music_likes ml ON f.user_id = ml.user_id) t1 LEFT JOIN (SELECT f.user_id, follower_id, ml.music_id "follower__likes__music_id" FROM follow f LEFT JOIN music_likes ml ON f.follower_id = ml.user_id) t2 ON t1.user_id = t2.user_id AND t1.follower_id = t2.follower_id
#第四步:根据条件筛选出关注者喜欢的音乐,不能和自己喜欢的重复:指定user_id = 1 ,特别注意筛选条件是关注者的音乐不在自己喜欢的音乐列表里(where子句)
SELECT DISTINCT follower__likes__music_id FROM (SELECT t1.user_id, t1.follower_id, IFNULL(user__likes__music_id,0) user__likes__music_id, IFNULL(follower__likes__music_id,0) follower__likes__music_id FROM (SELECT f.user_id, follower_id, ml.music_id "user__likes__music_id" FROM follow f LEFT JOIN music_likes ml ON f.user_id = ml.user_id) t1 LEFT JOIN (SELECT f.user_id, follower_id, ml.music_id "follower__likes__music_id" FROM follow f LEFT JOIN music_likes ml ON f.follower_id = ml.user_id) t2 ON t1.user_id = t2.user_id AND t1.follower_id = t2.follower_id) tt WHERE user_id = 1 AND follower__likes__music_id NOT IN (SELECT music_id FROM music_likes WHERE user_id = 1 )
#第五步:左连接音乐表即可
SELECT music_name FROM (SELECT DISTINCT follower__likes__music_id FROM (SELECT t1.user_id, t1.follower_id, IFNULL(user__likes__music_id,0) user__likes__music_id, IFNULL(follower__likes__music_id,0) follower__likes__music_id FROM (SELECT f.user_id, follower_id, ml.music_id "user__likes__music_id" FROM follow f LEFT JOIN music_likes ml ON f.user_id = ml.user_id) t1 LEFT JOIN (SELECT f.user_id, follower_id, ml.music_id "follower__likes__music_id" FROM follow f LEFT JOIN music_likes ml ON f.follower_id = ml.user_id) t2 ON t1.user_id = t2.user_id AND t1.follower_id = t2.follower_id) tt WHERE user_id = 1 AND follower__likes__music_id NOT IN (SELECT music_id FROM music_likes WHERE user_id = 1 )) ttt LEFT JOIN music m ON ttt.follower__likes__music_id = m.id ORDER BY m.id