简体   繁体   English

获取朋友的个人资料信息

[英]Get profile posts from friends

I'm making a small CMS, where you can have friends, and post on their walls. 我正在制作一个小型CMS,您可以在这里与朋友聊天并张贴在他们的墙上。 I'm making a page where you can view all the posts your friends have posted (on their profiles) however, I cannot get it to properly function. 我正在创建一个页面,您可以在其中查看您朋友发布的所有帖子(在他们的个人资料上),但是,我无法使其正常运行。

Table: friends 表:朋友

+---------------+---------+------+-----+---------+----------------+
| Field         | Type    | Null | Key | Default | Extra          |
+---------------+---------+------+-----+---------+----------------+
| friend_id     | int(10) | NO   | PRI | NULL    | auto_increment |
| user1_id      | int(10) | NO   |     | NULL    |                |
| user2_id      | int(10) | NO   |     | NULL    |                |
| friends_since | int(10) | NO   |     | NULL    |                |
+---------------+---------+------+-----+---------+----------------+

Table: profile_posts 表格:profile_posts

+----------------+---------+------+-----+---------+----------------+
| Field          | Type    | Null | Key | Default | Extra          |
+----------------+---------+------+-----+---------+----------------+
| post_id        | int(12) | NO   | PRI | NULL    | auto_increment |
| posted_user_id | int(12) | YES  |     | NULL    |                |
| user_id        | int(12) | YES  |     | NULL    |                |
| date_posted    | int(11) | YES  |     | NULL    |                |
| total_likes    | int(12) | YES  |     | NULL    |                |
| users_liked    | text    | YES  |     | NULL    |                |
| total_dislikes | int(12) | YES  |     | NULL    |                |
| users_dislikes | text    | YES  |     | NULL    |                |
| message        | text    | YES  |     | NULL    |                |
+----------------+---------+------+-----+---------+----------------+

Basically, if I want to determine if a user has posted on their profile as a status, the posted_user_id and user_id columns in the table profile_posts have to match (posted_user_id is the user's id of the post being posted on, and user_id is the user_id is the id of the person who posted that message). 基本上,如果我想确定用户是否已将其个人资料发布为状态,则表profile_posts中的post_user_id和user_id列必须匹配(posted_user_id是要发布的帖子的用户ID,user_id是user_id是发布该消息的人的ID)。

However, I've tried multiple times to get friend's statuses. 但是,我已经尝试过多次以获取朋友的身份。

SELECT *
FROM `friends` AS `f`
LEFT JOIN `profile_posts` AS `pp`
    ON `pp`.posted_user_id = `pp`.user_id
WHERE 
    `f`.user1_id != 2 OR
    `f`.user2_id != 2
ORDER BY `pp`.`date_posted` DESC
LIMIT 0,15;

(Hurray for not working! :D) (万分感谢!!:D)

SELECT `f`.*,`pp`.*,
    IF(`f`.user1_id=2, `f`.user2_id, `f`.user1_id) AS `uid`
FROM `friends` AS `f`
LEFT JOIN `profile_posts` AS `pp`
    ON `pp`.posted_user_id =`uid`
WHERE 
    `f`.user1_id = `uid` OR
    `f`.user2_id = `uid`
ORDER BY `pp`.`date_posted` DESC
LIMIT 0,15;

I'm not sure how to do this honestly, It's starting to make me rage as I cannot figure how to do this. 我不确定如何诚实地做这件事,这使我很生气,因为我不知道该怎么做。 :/ :/

Never mind, figured out how to do it. 没关系,想通了怎么做。 :D :d

EDIT: 编辑:

SELECT 
    `f`.*,
    `pp`.*,
    `ai`.`name`,
    `ai`.`lastname`
FROM `friends` AS `f`
LEFT JOIN `profile_posts` AS `pp`
    ON
        `pp`.`posted_user_id` = `pp`.`user_id` AND
        `pp`.`user_id` = `f`.user1_id OR
        `pp`.`user_id` = `f`.user2_id
LEFT JOIN `account_info` AS `ai`
    ON `ai`.`u_id` = `pp`.`user_id`
WHERE
    `pp`.`user_id` = `pp`.`posted_user_id` AND
    `f`.user1_id = 1 OR
    `f`.user2_id = 1 AND
    `pp`.`user_id` = `pp`.`posted_user_id`
GROUP BY `pp`.post_id
ORDER BY `pp`.date_posted DESC
LIMIT 0,20;

Selects the current user's friends, then it grabs all profile posts of their friends and then joins the account information table to get the name, and other information. 选择当前用户的朋友,然后获取其朋友的所有个人资料,然后加入帐户信息表以获取姓名和其他信息。

I'm trying to see if I can make this smaller, if not better.. 我正在尝试看看是否可以将其缩小甚至更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM