【问题标题】:Join 2 MySQL Tables to list all posts and find users followed by a particular user (Logged INN User)加入 2 个 MySQL 表以列出所有帖子并查找特定用户 (Logged INN User) 关注的用户
【发布时间】:2018-11-18 21:48:29
【问题描述】:

我的项目中有两个表。

POSTS 表

p_id     p_user_id      p_title     p_description
.................................................
1        1              Post 1      lorem ipsum * 1
2        1              Post 2      lorem ipsum * 2
3        2              Post 3      lorem ipsum * 3
4        3              Post 4      lorem ipsum * 4

FOLLOWERS 表

f_id     f_following_users_id    f_followed_users_id     f_date
...................................................................
1        2                       1                       2018-01-25
2        2                       3                       2018-01-25
3        3                       2                       2018-01-25

现在我想根据登录的用户 ID 获取所有用户的列表。登录用户 id 取自 $_SESSION['user_id']。

我想要的结果如下。

第一种情况:如果登录用户的user_id为1,即$_SESSION['user_id'] = 1,或者第二张表的following_users_id为1,结果应该是:

Array
(
[0] => Array
    (
        [p_id] => 1
        [p_user_id] => 1
        [p_title] => Post 1
        [p_description] => lorem ipsum * 1
        [f_id] => 
        [f_following_users_id] => 
        [f_followed_users_id] => 
        [f_date] =>
    )

[1] => Array
    (
        [p_id] => 2
        [p_user_id] => 1
        [p_title] => Post 2
        [p_description] => lorem ipsum * 2
        [f_id] => 
        [f_following_users_id] => 
        [f_followed_users_id] => 
        [f_date] => 
    )

[2] => Array
    (
        [p_id] => 3
        [p_user_id] => 2
        [p_title] => Post 3
        [p_description] => lorem ipsum * 3
        [f_id] => 
        [f_following_users_id] => 
        [f_followed_users_id] => 
        [f_date] => 
    )
    
)

数组的最后四个字段应为空,因为具有 user_id 的用户尚未关注任何人

第二种情况:如果登录用户user_id为2,即$_SESSION['user_id'] = 2,或者第二张表的following_users_id为2,结果应该是:

Array
(
[0] => Array
    (
        [p_id] => 1
        [p_user_id] => 1
        [p_title] => Post 1
        [p_description] => lorem ipsum * 1
        [f_id] => 1
        [f_following_users_id] => 2 
        [f_followed_users_id] => 1
        [f_date] => 2018-01-25
    )

[1] => Array
    (
        [p_id] => 2
        [p_user_id] => 1
        [p_title] => Post 2
        [p_description] => lorem ipsum * 2
        [f_id] => 1
        [f_following_users_id] => 2 
        [f_followed_users_id] => 1
        [f_date] => 2018-01-25
    )

[2] => Array
    (
        [p_id] => 3
        [p_user_id] => 2
        [p_title] => Post 3
        [p_description] => lorem ipsum * 3
        [f_id] => 
        [f_following_users_id] => 
        [f_followed_users_id] => 
        [f_date] => 
    )

)

前 2 个数组的前四个字段应填充关注者表的第一行,因为用户 ID 的用户已关注用户 ID 为 1 的用户。第三个帖子的 4 行应为空白,因为用户 ID 为 2 的用户没有' t 关注 user_id 为 2 的用户**

第三种情况:如果登录用户user_id为3,即$_SESSION['user_id'] = 3,或者第二张表的following_users_id为3,结果应该是:

Array
(
[0] => Array
    (
        [p_id] => 1
        [p_user_id] => 1
        [p_title] => Post 1
        [p_description] => lorem ipsum * 1
        [f_id] => 
        [f_following_users_id] => 
        [f_followed_users_id] => 
        [f_date] => 
    )

[1] => Array
    (
        [p_id] => 2
        [p_user_id] => 1
        [p_title] => Post 2
        [p_description] => lorem ipsum * 2
        [f_id] => 
        [f_following_users_id] =>  
        [f_followed_users_id] => 
        [f_date] => 
    )

[2] => Array
    (
        [p_id] => 3
        [p_user_id] => 2
        [p_title] => Post 3
        [p_description] => lorem ipsum * 3
        [f_id] => 3
        [f_following_users_id] => 3
        [f_followed_users_id] => 2
        [f_date] => 2018-01-25
    )

)

前 2 个数组 4 最后一列应该为空,因为 User_id 3 没有关注这些帖子用户。但是第三个数组最后四列应该被填充,因为 user_id 3 已经跟随 user_id 2。

我已经搜索了所有解决方案,但徒劳无功。也许我什至在 Stack Overflow 上都找不到正确的帖子。

我尝试的 MySQL 查询是:

SELECT * FROM posts AS p LEFT JOIN followers AS f ON p.p_user_id=f.f_followed_users_id WHERE f.f_followed_users_id = p_user_id AND f.f_following_users_id = 1 ORDER BY p.p_id

但我没有得到我想要的结果。我尝试使用左连接右连接外部和内部连接。但没有成功。我可以随意加入这两张桌子吗?

【问题讨论】:

    标签: mysql join left-join


    【解决方案1】:

    我觉得您可以尝试循环而不是加入表格,因为您的关注者表格将只有一行满足您的条件。

    所以你为什么不这样尝试:

    1) 从您的数据库中获取所有帖子。 2)对于您检索到的每个帖子,根据您的条件从关注者表中获取数据。 3)创建一个新数组。 4)如果步骤2返回结果,则将关注者表数据与帖子一起合并到新数组中。 5) 否则将帖子本身合并到新数组中。

    完成每个循环后,尝试打印它并使用以下方法检查数据,

    echo "<pre>";
    print_r($new_array);
    exit;
    

    试试这个,如果您需要任何进一步的帮助,请告诉我

    【讨论】:

    • 终于有回应了。让我试试这个,会让你知道的。明白了这个概念。会问你我是否还有任何疑问。谢谢你的时间:)
    • 那个工作的人!无论如何,当我的数据库增长时,我觉得这会很慢。无论如何,谢谢你的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2022-01-11
    • 2012-02-03
    相关资源
    最近更新 更多