如何选择相关帖子(类似于 Facebook 的墙)?

发布于 2024-11-19 00:39:32 字数 3836 浏览 6 评论 0原文

我有一些类似 Facebook 的 Wall 构建在 PHP 上,使用 MySQL 数据库。

结构:

  • 类别由管理员创建,不应该经常更改,
  • 组由用户创建。它们的数量可能非常巨大。他们就像一个类别的“孩子”,
  • 帖子也是由用户发布的。他们就像一个群体的“孩子”,而那个群体就像一个类别的“孩子”;

最后一件事是评论。它们存储在帖子所在的同一个表中,但“回复”行设置为帖子的 ID,就像它的“父级”一样。

这是一个简单的例子:

Food (category):

-> Kebabs (group)

->-> What's your fave kebab, folks? (post)

->->-> I love doner kebab! (post too, but displayed as comment)

我在选择相关帖子时遇到了麻烦。你看,我需要显示与用户有某种关系的帖子。

目前这些关系是:

  • 用户是帖子的作者,
  • 用户对帖子发表了评论(该帖子可能不是他自己发表的),
  • 另一个用户在“我们的”用户所属的组中发表了评论;

我想这将是一个复杂的查询......而我的知识太少了。

以下是查询:

SELECT `posts`.`id`, `posts`.`created_at`, `posts`.`content`, `posts`.`replies`, `groups`.`id` AS `group_id`, `groups`.`name` AS `group_name`, `users`.`name`, `users`.`surname`, `users`.`avatar`
FROM `posts`
JOIN `groups` ON (`groups`.`id` = `posts`.`group_id`)
JOIN `users` ON (`users`.`id` = `posts`.`user_id`)
WHERE `posts`.`status` = 1 AND `posts`.`post_id` = 0 AND `posts`.`user_id` = '33'
ORDER BY `posts`.`id` DESC
LIMIT 10
OFFSET 0

编辑:

以下是表结构:

CREATE TABLE `bio_community_categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CREATE TABLE `bio_community_groups` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `category_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `created_at` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;

CREATE TABLE `bio_community_posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `group_id` int(10) unsigned NOT NULL,
  `post_id` int(11) unsigned NOT NULL DEFAULT '0',
  `created_at` int(11) NOT NULL,
  `content` text NOT NULL,
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `replies` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `status` (`status`),
  KEY `post_id` (`post_id`),
  KEY `user_id` (`user_id`),
  KEY `group_id` (`group_id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `surname` varchar(50) NOT NULL,
  /* Etc.. */
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `bio_community_categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CREATE TABLE `bio_community_groups` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `category_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `created_at` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;

CREATE TABLE `bio_community_posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `group_id` int(10) unsigned NOT NULL,
  `post_id` int(11) unsigned NOT NULL DEFAULT '0',
  `created_at` int(11) NOT NULL,
  `content` text NOT NULL,
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `replies` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `status` (`status`),
  KEY `post_id` (`post_id`),
  KEY `user_id` (`user_id`),
  KEY `group_id` (`group_id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `surname` varchar(50) NOT NULL,
  /* Etc.. */
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I have something like Facebook's Wall build on PHP that uses MySQL database.

Structure:

  • Categories are made by admins and aren't meant to be changed often,
  • Groups are made by users. Count of them can be very huge. They are like "child" of one category,
  • Posts are made by users too. They are like "child" of one group and that group is like "child" of one category;

Last thing is comments. They are stored in the same table where posts are, but with "reply" row set to post's ID that's like "parent" of it.

Here are simple example:

Food (category):

-> Kebabs (group)

->-> What's your fave kebab, folks? (post)

->->-> I love doner kebab! (post too, but displayed as comment)

I have troubles when selecting related posts. You see, I need to display posts that are somehow related to user.

These relationships at the moment are:

  • User is author of post,
  • User has commented on post (that post may not be made by himself),
  • Another user is commented on group where "our" user is member;

I guess that will be one complex query... and my knowledge is little too short.

Here are the query:

SELECT `posts`.`id`, `posts`.`created_at`, `posts`.`content`, `posts`.`replies`, `groups`.`id` AS `group_id`, `groups`.`name` AS `group_name`, `users`.`name`, `users`.`surname`, `users`.`avatar`
FROM `posts`
JOIN `groups` ON (`groups`.`id` = `posts`.`group_id`)
JOIN `users` ON (`users`.`id` = `posts`.`user_id`)
WHERE `posts`.`status` = 1 AND `posts`.`post_id` = 0 AND `posts`.`user_id` = '33'
ORDER BY `posts`.`id` DESC
LIMIT 10
OFFSET 0

Edit:

Here is the table structure:

CREATE TABLE `bio_community_categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CREATE TABLE `bio_community_groups` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `category_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `created_at` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;

CREATE TABLE `bio_community_posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `group_id` int(10) unsigned NOT NULL,
  `post_id` int(11) unsigned NOT NULL DEFAULT '0',
  `created_at` int(11) NOT NULL,
  `content` text NOT NULL,
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `replies` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `status` (`status`),
  KEY `post_id` (`post_id`),
  KEY `user_id` (`user_id`),
  KEY `group_id` (`group_id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `surname` varchar(50) NOT NULL,
  /* Etc.. */
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `bio_community_categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CREATE TABLE `bio_community_groups` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `category_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `created_at` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;

CREATE TABLE `bio_community_posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `group_id` int(10) unsigned NOT NULL,
  `post_id` int(11) unsigned NOT NULL DEFAULT '0',
  `created_at` int(11) NOT NULL,
  `content` text NOT NULL,
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `replies` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `status` (`status`),
  KEY `post_id` (`post_id`),
  KEY `user_id` (`user_id`),
  KEY `group_id` (`group_id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `surname` varchar(50) NOT NULL,
  /* Etc.. */
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮凉 2024-11-26 00:39:33

假设也会有组用户关系表,
让我们说“groups_members”,因此您的查询将如下所示,

SELECT `posts`.`id`, `posts`.`created_at`, `posts`.`content`, `posts`.`replies`, `groups`.`id` AS `group_id`, `groups`.`name` AS `group_name`, `users`.`name`, `users`.`surname`, `users`.`avatar`
FROM `posts`
JOIN `groups` ON (`groups`.`id` = `posts`.`group_id`)
JOIN `users` ON (`users`.`id` = `posts`.`user_id`)
WHERE `posts`.`status` = 1 AND `posts`.`post_id` = 0 AND  (`posts`.`user_id` = '33' OR `bio_community_posts`.`group_id` IN (SELECT `group_id` FROM `groups_members` WHERE `user_id` =33)) 
ORDER BY `posts`.`id` DESC
LIMIT 10
OFFSET 0

从上面的查询中,您将获得用户 33 发布的帖子以及用户 33 所属群组的帖子。

Assume there will be group user relation table too,
Let us say "groups_members" so your query will be as follows,

SELECT `posts`.`id`, `posts`.`created_at`, `posts`.`content`, `posts`.`replies`, `groups`.`id` AS `group_id`, `groups`.`name` AS `group_name`, `users`.`name`, `users`.`surname`, `users`.`avatar`
FROM `posts`
JOIN `groups` ON (`groups`.`id` = `posts`.`group_id`)
JOIN `users` ON (`users`.`id` = `posts`.`user_id`)
WHERE `posts`.`status` = 1 AND `posts`.`post_id` = 0 AND  (`posts`.`user_id` = '33' OR `bio_community_posts`.`group_id` IN (SELECT `group_id` FROM `groups_members` WHERE `user_id` =33)) 
ORDER BY `posts`.`id` DESC
LIMIT 10
OFFSET 0

From the above query you will get the posts posted by the user 33 + those posts from the group in which the user 33 is a member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文