duanli6834 2019-07-14 12:03
浏览 99
已采纳

如何从多个用户一起从两个不同的表中获取数据?

I have 1 table for the user, 1 for comments and another for friends below is the table structure :

Comments Table :

------------------------------------------------
| comment_id  | user_id_c | commentstatus      | 
------------------------------------------------
|  1          |   1       |   Sample comment 1 |
|  2          |   2       |   Sample comment 2 |
------------------------------------------------

User Table :

------------------------------------------------------
| id  | username            | password  |  Full name |
------------------------------------------------------
|  1  |   user1 (loggedin)  | Sample 1  |  John      |
|  2  |   user2             | Sample 2  |  Smith     |
|  3  |   user3             | Sample 3  |  Andrew    |
|  4  |   user4             | Sample 4  |  Victor    |
|  3  |   user5             | Sample 3  |  Robert    |  
-------------------------------------------------------

Friends Tables

---------------------------------------------------
| id  | friend1             | friend2   |   status   
---------------------------------------------------
|  1  |   user1             | user3     |  friend
|  2  |   user1             | user5     |  friend
|  3  |   user2             | user4     |  friend
---------------------------------------------------

currently, if any user posts a comment it gets inserted there in the database inside comments table. now if I am logged in I can fetch all my comments from the database.

To explain I would like to inform that you all can see, user 1 is friend with user 3 and user 5 according to the friends table.

All I want is to fetch the comments for the logged in user and for those who are the friend with the logged in user. however, I am unable to figure it out that how can I fetch the comments of loggedin user and it's friends all together.

the script I am using to fetch my posts is as follows :

public function comments(){
            global $pdo;
            $query = $pdo->prepare("SELECT u.*, c.* FROM users u INNER JOIN comments c ON u.id = c.user_id_c WHERE u.id = ".$_SESSION['sid']." ORDER BY c.comment_id DESC");
            $query->execute();
            return $query->fetchAll();
        }

and then fetching comments using below code :

$comments  = $get->comments();

Then :

foreach($comments as $row){

echo $row['Full name']; //user name from user table in the database
echo $row['commentstatus']; //comment from comments table in the database

It is fetching data for the logged in user perfectly, Is it possible anyway that I can fetch the comments from the friends of the logged in user also with logged in user's comments from the database at the same time.

  • 写回答

1条回答 默认 最新

  • duangu6588 2019-07-14 12:20
    关注

    You are looking for something like this:

    $query = $pdo->prepare("SELECT u.*, c.* FROM users u INNER JOIN comments c ON u.id = c.user_id_c WHERE u.id = ".$_SESSION['sid']." OR c.user_id IN (SELECT friend_id FROM friends WHERE friends.user_id = ".$_SESSION['sid'].") ORDER BY c.comments_id DESC"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化