LeetCode(数据库)- 兴趣相同的朋友

Posted 程序员牧码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 兴趣相同的朋友相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

题目大意:略。

解题思路:略。

AC 代码

-- 解决方案(1)
select distinct a.user1_id, a.user2_id
  from Friendship a, Listens b, Listens c
 where a.user1_id = b.user_id
   AND a.user2_id = c.user_id
   AND b.song_id = c.song_id
   AND b.day = c.day
 group by a.user1_id, a.user2_id, b.day
having COUNT(distinct b.song_id) >= 3

-- 解决方案(2)
WITH t AS(SELECT * FROM Listens GROUP BY 1, 2, 3)

SELECT DISTINCT l1.user_id user1_id, l2.user_id user2_id
FROM t l1 JOIN t l2 ON l1.user_id < l2.user_id AND l1.day = l2.day
WHERE (l1.user_id, l2.user_id) IN (SELECT * FROM Friendship)
GROUP BY 1, 2, l1.day
HAVING COUNT(IF(l1.song_id = l2.song_id, 1, NULL)) >= 3

以上是关于LeetCode(数据库)- 兴趣相同的朋友的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-面试算法经典-Java实现106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)(示例(代码片

Leetcode刷题Python LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色

根据对象列表生成新卡片

Android 返回堆栈管理打印 Android 中当前运行的 Activity 任务栈信息 | Activity 任务栈信息分析 | Activity 在相同 Stack 中的不同 Task(代码片

牛刀小试 ——《当选者》LeetCode Plus 会员专享题详细解析Hive / MySQL

关于跑满指定CPU时间片的一些细节问题