Hi IM trying to find out how i can get mutual friends

你好IM试图找出我如何能够得到共同的朋友

im currently having problems thinking this out.

我目前在思考这个问题时遇到了问题

I have a table called "users" and this is how it looks

我有一个名为“用户”的表,这就是它的外观

id | name
-----------
1   Kenny
2   Jack
3   Jimmy
4   Chris
5   Meg
6   Jake
7   Micheal
8   Dude

I have a table called "friendship" and this is how it looks

我有一张名为“友谊”的桌子,这就是它的样子

user_a | user_b 
----------------
4         1        
7         5
8         1
2         4
2         1
5         2
1         6
1         7

user_a sends user_b a request to become friends and... BAM there are friends. Now if im user 2, and i go to user 1s friend list, i want to see what friends we have in common. What is the correct sql to do that?

user_a向user_b发送成为朋友的请求... BAM有朋友。现在,如果用户2,我去用户1的朋友列表,我想看看我们有什么共同的朋友。这样做的正确sql是什么?

1 个解决方案

#1


2

Assuming that it is not possible for User 1 to be friends with User 7 twice, (meaning there can not be a row with user_a = 1, user_b=7 and another row user_a = 7, user_b = 1).

假设用户1不可能两次成为用户7的朋友,(意味着不能有user_a = 1的行,user_b = 7而另一行user_a = 7,user_b = 1)。

SELECT IF(user_a = 1 OR user_a = 2, user_b, user_a) friend
FROM friendship
WHERE (user_a = 1 OR user_a = 2) OR (user_b = 1 OR user_b = 2)
GROUP BY 1
HAVING COUNT(*) > 1

更多相关文章

  1. 记一次无备份恢复Mysql误删用户数据
  2. MySQL root 用户密码重置
  3. Mysql5.7.10新加用户
  4. SQL查询 - 从表中查找不同的用户
  5. jsp+Mysql实现简单用户登陆
  6. DRP系统中用户增删改查(分页)Model1模式
  7. CentOS下MySQL的安装和修改用户密码
  8. Android 修改用户图像笔记
  9. Android记录4--自定义ToggleButton+用SharedPreferences保存用户

随机推荐

  1. [Android Pro] Android 打包流程
  2. Android 两种制作圆形/圆角图片的方法
  3. Android - 在一个应用程序中启动另外一个
  4. Kotlin控制流、返回与跳转
  5. Android添加USB连接提示音(SettingsProvid
  6. 如何在Android设备中创建Web服务器(用于远
  7. android应用私有存储文件的写入与读取-op
  8. 如何从Android中的人脸检测部分找出脸部
  9. 为什么我们应该将XMPP而不是JSON用于简单
  10. ViewPager滚动很卡的问题