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

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

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

Comments Table :

  1. ------------------------------------------------
  2. | comment_id | user_id_c | commentstatus |
  3. ------------------------------------------------
  4. | 1 | 1 | Sample comment 1 |
  5. | 2 | 2 | Sample comment 2 |
  6. ------------------------------------------------

User Table :

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

Friends Tables

  1. ---------------------------------------------------
  2. | id | friend1 | friend2 | status
  3. ---------------------------------------------------
  4. | 1 | user1 | user3 | friend
  5. | 2 | user1 | user5 | friend
  6. | 3 | user2 | user4 | friend
  7. ---------------------------------------------------

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 :

  1. public function comments(){
  2. global $pdo;
  3. $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");
  4. $query->execute();
  5. return $query->fetchAll();
  6. }

and then fetching comments using below code :

$comments  = $get->comments();

Then :

  1. foreach($comments as $row){
  2. echo $row['Full name']; //user name from user table in the database
  3. 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 04: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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部