sql - "people you may know"sql查询

标签 sql mysql database sqlite

我正在开发“您可能认识的人”功能。我有两个表:

用户
编号
电子邮件
姓名
等等

友谊
用户编号
friend_id

对于每一份友谊,我都会做两份记录。假设用户 7 和 9 成为 friend ...我会在友谊表中记录 user_id=7、friend_id=9 和另一个 user_id=9、friend_id=7。

我如何进行 sql 查询以根据我 friend 的 friend 推荐我可能认识的人?我还希望它根据最共同的 friend 排序。

最佳答案

select u.id, u.email, u.name, u.etc
-- Get all my friends
from Friendships as f1
-- Get their friends
inner join Friendships as f2
    on f1.friend_id = f2.user_id
-- Get their friends User information
inner join Users as u
    on f2.friend_id = u.id
where f1.user_id = @userId

将是我开始的地方。

关于sql - "people you may know"sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4147482/

相关文章:

mysql - 源为 Mysql 的报告速度更快

sql - 如何在 SQL Server 2008 中将 int 转换为日期

SQL 足球积分表最近 5 场比赛

mysql - 在表中的更新列上创建触发器

mysql - 我如何从包含多个值的表的列中搜索具有多个值的搜索键

php - 从 MySQL 数据库中预选的下拉列表值

mysql - 如何对 3 个表进行 1 次查询

database - 版本控制/记录对数据库模型的更改

sql - Oracle 根据输入参数更新字段 A 或字段 B

database - 使用 Rest api 做分页数据库的更好方法是什么