如何选择我或我的朋友在新闻Feed中创建的帖子?

时间:2014-09-20 00:24:39

标签: mysql sql

我一直试图这样做很长一段时间然后我认为这是SQL查询,我无法让它在第一时间正常工作。

我正在尝试创建一个类似于Facebook的简单新闻Feed页面,您可以在其中查看您在个人资料中发布的帖子以及您的朋友个人资料,还可以查看您的朋友在您的个人资料上发布的帖子以及他们的帖子自己的个人资料。

SELECT 
    friendsTbl.profile_one,
    friendsTbl.profile_two,
    user_profile.u_id as my_uid,
    user_profile.f_name as my_fname,
    user_profile.l_name as my_lname,
    friend_profile.u_id as friend_uid,
    friend_profile.f_name as friend_fname,
    friend_profile.l_name as friend_lname,
    profilePostTbl.post as post,
    profilePostTbl.post_to as posted_profile,
    profilePostTbl.post_by as posted_by
FROM friendsTbl
LEFT JOIN profileTbl AS user_profile ON user_profile.profile_name = IF(friendsTbl.profile_one = 'john123', friendsTbl.profile_one, friendsTbl.profile_two)
LEFT JOIN profileTbl AS friend_profile ON friend_profile.profile_name = IF(friendsTbl.profile_one = 'john123', friendsTbl.profile_two, friendsTbl.profile_one)
LEFT JOIN profilePostTbl ON (post_by = IF(profilePostTbl.post_to = friendsTbl.profile_one,profilePostTbl.post_to, profilePostTbl.post_by))
WHERE friendsTbl.profile_one = 'john123' OR friendsTbl.profile_two = 'john123'

这是一个小提琴:http://sqlfiddle.com/#!2/a10f39/1

对于此示例,john123是当前登录的用户,并且是hassey,smith和joee的朋友,因此只有那些帖子必须显示在john123在他自己或他朋友的帖子上发布的新闻Feed中以及他的朋友在他们自己的个人资料以及john123的个人档案中发布的内容。

此问题是对PHP Sub-query to select all user's profile who are friends with me in Friends table的跟进。

2 个答案:

答案 0 :(得分:6)

我知道你已经接受了答案,但是我写了一半,所以我决定发布它。

我希望在回答你的问题之前回去一点。在开发应用程序和构建数据库时,您应该 始终 尝试将事物构造为尽可能具有描述性和紧凑性。有一个名为color的变量/列并在那里存储加密的用户密码真是很尴尬(很奇怪,对吧?)。有一些标准的数据库命名约定,如果遵循这些约定,在开发复杂的应用程序时特别容易。我建议你阅读一些关于命名约定的博客。一个好的起点可能是this一个。

我完全意识到,根据下面建议的更改,您可能需要部分/完全重写您迄今为止所编写的应用程序代码,但如果您真的想要更好地工作,那么这取决于您。< / p>

让我们从修复数据库结构开始。从它的外观来看,你正在做一个类似于facebook的新闻源的应用程序。在这种情况下,使用FOREIGN KEYS几乎是强制性的,因此您可以保证一些数据的一致性。下面的示例数据库模式显示了如何实现这一点。

-- Application users are stored here.
CREATE TABLE users (
  user_id      INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  first_name   VARCHAR(255),
  last_name    VARCHAR(255),
  profile_name VARCHAR(255)
) ENGINE=InnoDb;

-- User friendship relations go here
CREATE TABLE friends (
  friend_id   INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  profile_one INT NOT NULL,
  profile_two INT NOT NULL,
  FOREIGN KEY (profile_one) REFERENCES users (user_id),
  FOREIGN KEY (profile_two) REFERENCES users (user_id)
) ENGINE=InnoDb;

-- User status updates go here
-- This is what will be displayed on the "newsfeed"
CREATE TABLE statuses (
  status_id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  author_id    INT NOT NULL,
  recipient_id INT NOT NULL,
  message      TEXT,
  -- created date ?
  -- last updated date ?
  FOREIGN KEY (author_id)    REFERENCES users (user_id),
  FOREIGN KEY (recipient_id) REFERENCES users (user_id)
) ENGINE=InnoDb;

-- Replies to user statuses go here. (facebook style..)
-- This will be displayed as the response of a user to a certain status
-- regardless of the status's author.
CREATE TABLE replies (
  reply_id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  status_id INT NOT NULL,
  author_id INT NOT NULL,
  message   TEXT,
  FOREIGN KEY (status_id) REFERENCES statuses (status_id),
  FOREIGN KEY (author_id) REFERENCES users    (user_id)
) ENGINE=InnoDb;

现在这已经修复,我们可以继续下一步 - 选择john123(谁拥有user_id=1)的新闻源。这可以通过以下查询来实现:

SET @search_id:=1; -- this variable contains the currently logged in user_id so that we don't need to replace the value more than once in the whole query.

SELECT 
    statuses.*,
    author.first_name     AS author_first_name,
    author.last_name      AS author_last_name,
    recipient.first_name  AS recipient_first_name,
    recipient.last_name   AS recipient_last_name
FROM statuses
JOIN users AS author      ON author.user_id    = statuses.author_id
JOIN users AS recipient   ON recipient.user_id = statuses.recipient_id
WHERE (statuses.author_id = @search_id OR statuses.recipient_id = @search_id)
ORDER BY status_id ASC

here你可以在一个方形小组中看到它。正如您所看到的,只是通过更好地构建数据库,我已经消除了对子查询的需求(这是EXISTS / NOT EXISTS根据docsEXPLAIN执行的操作。此外,上面的SQL代码将更容易维护和扩展。

无论如何,我希望你觉得这很有用。

答案 1 :(得分:3)

我认为您需要从帖子开始并使用EXISTS将结果限制为相关的结果:

SELECT * FROM
profilePostTbl post
WHERE 
 -- This post is by a friend of current user or his own post.
 EXISTS (SELECT * FROM friendsTbl WHERE 
         post.post_by IN (profile_one, profile_two)
         AND 'john123' IN (profile_one, profile_two))
 -- This post is addressing the current user
 OR post_to = 'john123';

如果您想呈现帖子作者和收件人的姓名,只需将他们加入帖子:

SELECT post.*,   
    post_by.u_id as by_uid,
    post_by.f_name as by_fname,
    post_by.l_name as by_lname,
    post_to.u_id as to_uid,
    post_to.f_name as to_fname,
    post_to.l_name as to_lname
FROM
  profilePostTbl post INNER JOIN 
  profileTbl post_by ON post.post_by = post_by.profile_name INNER JOIN
  profileTbl post_to ON post.post_to = post_to.profile_name 
WHERE 
 -- This post is by a friend of current user or his own post.
 EXISTS (SELECT * FROM friendsTbl WHERE 
         post.post_by IN (profile_one, profile_two)
         AND 'john123' IN (profile_one, profile_two))
 -- This post is addressing the current user
 OR post_to = 'john123';