MySQL : How to identify users I follow or follow me from other person's followers?

标签 mysql sql laravel eloquent

我正在尝试创建一个类似 Twitter 的关注/关注系统。表格设计是这样的 -

leader_id | following_id | is_mutual
        1 |            2 | 1 // Mutually follow each other
        2 |            1 | 1
        1 |            3 | 0 
        1 |            4 | 1
        4 |            1 | 1

假设我是 user_id=4。我正在查看 user_id=1 的关注者。我会查看列表 -

2
3
4 -> me

如何确定我正在关注哪些用户或正在关注我。我想为此写一篇 Laravel Eloquent 声明。甚至 MySQL 查询也能帮助我找出执行此操作的正确方法。

最佳答案

我不会走那条路。

而是有一个一对多的订阅关系表,例如

|subscriber  | subscribed_user_id 
---------------------------------
|1           | 2
|1           | 3
|2           | 3
|3           | 1

甚至是 json 编码的灵活列表

|subscriber  | subscribed_users
-------------------------------
|1           | {2,3}
|2           | {3}
|3           | {1}

关于MySQL : How to identify users I follow or follow me from other person's followers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59409631/

相关文章:

mysql - 优化包含左连接的 sql 查询

php - 比较 MySQL varchar

php - 图像上传到mysql数据库不起作用

mysql - column=null 的 SQL 查询返回零结果集

mysql - 计数(*)多个表

mysql - Laravel - 时区在数据库中保存了 5 小时

PHP 找不到 mySQL 数据库

mysql - 如何在MySql中并排放置两列而不将它们合并为一列

php - 在 Laravel 中如何在查询中不带参数的 groupBy Url

php - 如何在 Laravel 5.3 中设置 session