mysql - SQL获取所有不是我 friend 的 friend 的 friend

标签 mysql sql

我正在开发“可能的 friend ”功能。我需要显示所有不是我 friend 的 friend 的 friend ,也不要发送我或没有我的待处理请求

FRIENDSHIPS
user_id
friend_id
status  (0 = pending, 1 = approved) 

对于每一份友谊,我都会做两份记录。假设用户 1 和 2 成为 friend ......我会做一个记录 where user_id=1,friend_id=2和另一个 where user_id=2, friend_id=1friendships表。

当第一个用户发送请求时,将状态设置为 0,当 friend 接受请求时,我会将两行都更新为 1

我将如何根据我 friend 的 friend 提出建议“可能的 friend ”的 sql 查询?

最佳答案

给你...简单连接

SELECT F2.friend_id
FROM FRIENDSHIPS F
JOIN FRIENDSHIPS F2 ON F.friend_id = F2.user_id
WHERE F2.friend_id NOT IN (SELECT friend_ID FROM FRIENDSHIPS WHERE user_id = @user_id)
  AND F.user_id = @user_id

关于mysql - SQL获取所有不是我 friend 的 friend 的 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7819654/

相关文章:

sql - 多个和单个索引

mysql - 如何使分组优化?

sql - Oracle 选择索引优化

PHP PDO SELECT 查询不显示结果

mysql - ExtractValue mysql XML 返回空而不是 NULL

php - MySql Password_verify() 不工作?

mysql - 在 Windows 7 上将通配符与 mysqlimport 一起使用

C# UWP 查看两个数据库行

sql - postgres 相当于 all_constraints

sql - 超正常化的危险?