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 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的