【问题标题】:3 tables in one mysql query?一个mysql查询中有3个表?
【发布时间】:2017-01-27 05:16:54
【问题描述】:

我需要一些有关我的 mysql 查询代码的帮助!我有三个数据库,我正在尝试找到一种方法来发布所有三个数据库中的数据。

users
----------------------------------
user_id   username      email..etc
-----------------------------------
1         username
-----------------------------------


friends_db
-------------------------------------
user_id   friend_id   friend_active
--------------------------------------
   1          2               1
--------------------------------------


posts
-------------------------------------
post_id   user_id      post_content
-------------------------------------
1         1             this is a post
--------------------------------------

我想做的是创建一个朋友帖子列表,您可以在其中查看您的朋友(在friend_active 中为 1 的人)所做的所有帖子。

我的功能:

function getFriendsPosts()
{   

    require "config.php";

    $friends = $c->query ( "SELECT * FROM friends_db WHERE user_id=" . $_SESSION["user_id"] ." AND friend_active = 1" ) ; 

    if ( $friends->num_rows > 0 ) 
    {

        while( $row = $friends->fetch_assoc() ) 
        {
            $postData[] = array( "user_id" => $row[ "user_id" ], "friend_active" => $row[ "friend_active" ], "friend_id" => $row[ "friend_id" ] );
        }
    } else {
        echo "No Data To Show";
    }

    return $postData;

}

我有:

$friends = $c->query ( "SELECT * FROM friends_db WHERE user_id=" . $_SESSION["user_id"] ." AND friend_active = 1" ) ; 

这给了我有friend_active = 1的任何人的user_id

但是我不知道如何通过将friend_id 与他们自己的user_id 相关联来扩展此功能,以获取他们的用户名,然后他们的帖子也将在其中显示。

我可能过于复杂了,或者我可能不会,我不知道。 有谁知道我如何以我已经完成的相同格式执行此操作(我还有其他事情正在处理这种格式,我也需要这样做)?或者有什么建议?

编辑:

$friends = getFriendsPosts();

foreach ($friends as $friend) {
    ?>
    <div class="ui-corner-all custom-corners">
    <div id="searchpost">
    <?php 


    echo $friend['user_id']; 
    echo $friend['friend_id']; 
    echo $friend['friend_active'];


    ?>
    </div>
    </div>
    <?php 
}
?>

我喜欢:

echo 用户名(与friend_id/他们自己的用户ID 相关) 回显帖子数据(与 user_id 相关)

【问题讨论】:

    标签: php mysql database


    【解决方案1】:

    我假设您查询的friends_db 部分实际上是friends 表?我还假设这些表之间存在关系:

    如果是这样,您可以使用连接:

    select * from friends as f
    join users as u on u.user_id = f.user_id 
    join posts as p on p.user_id = f.user_id
    where f.user_id = ...
    

    或者,获取您朋友的帖子:

    select * from posts as p
    join users as u on u.user_id = p.user_id //gets users table where related to posts
    join friends as f on f.user_id = p.user_id //gets friends where related 
    join users as fu on fu.user_id = f.friend_id //gets users table where related to friend user_id 
    where u.user_id = ....//the sources user_id 
    

    【讨论】:

    • 是的,我正在尝试显示您的朋友(在朋友数据库中)的用户(在用户数据库中)发布的帖子(在帖子数据库中)。我可以将用户名与朋友数据库一起放入将其缩小到两个要访问的表,但是无论如何我都无法将两个数据库连接在一起。我对您给我的代码不太熟悉,您可以向我解释一下,以便我可以将其添加到帖子数据库中吗?谢谢你:)
    • 查看编辑。事后看来,这是一个模糊的问题。你发的帖子是特定 id 的朋友发的吧?
    • 编辑了我在我的索引上要发布的内容。 :)
    猜你喜欢
    • 1970-01-01
    • 2014-11-14
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多