[英]How to structure database for unread item alerts per user
I just have a general database theory question. 我只是有一个通用的数据库理论问题。 I have a need to make something similar to showing what posts/items a user has viewed or not (such as in a forum) or an unread email message.
我需要制作类似于显示用户观看过的帖子/项目(例如在论坛中)或未读电子邮件的内容。 What I have is there are posts that multiple users can view, but it needs to separate by user who has actually viewed it.
我所拥有的是有多个用户可以查看的帖子,但它需要由实际查看过的用户分开。 So if User A viewed Post 1, it would no longer show that Post 1 is a new item to view, but to User B, it would still show that Post 1 is a new item to view.
因此,如果用户A查看了帖子1,它将不再显示帖子1是要查看的新项目,但是对于用户B,它仍然会显示帖子1是要查看的新项目。
I've search for other ideas and one of them is to get a timestamp of when the user last logged in, but I actually need to keep track of the posts they've seen as opposed to posts that have happened since they last logged in. 我已经搜索了其他想法,其中一个是获取用户上次登录时间的时间戳,但实际上我需要跟踪他们看到的帖子,而不是自上次登录后发生的帖子。
I would like a MySQL database solution if possible, but I'm open to cookies if that is a must. 如果可能的话,我想要一个MySQL数据库解决方案,但如果必须的话,我愿意接受cookie。 I could do this on my own and just figure it out, but I'd appreciate any advice on how to properly structure a table(s) to make this the most efficient.
我可以自己完成这个并且弄清楚,但我很欣赏任何有关如何正确构建表格以使其最有效的建议。 Also, bandwidth and storage is not issue.
此外,带宽和存储不是问题。
While reviewing the relevant schema for phpBB , I found the following: 在查看phpBB的相关模式时 ,我发现了以下内容:
# Table: 'phpbb_topics_track'
CREATE TABLE phpbb_topics_track (
user_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
topic_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
forum_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
mark_time int(11) UNSIGNED DEFAULT '0' NOT NULL,
PRIMARY KEY (user_id, topic_id),
KEY topic_id (topic_id),
KEY forum_id (forum_id)
) CHARACTER SET `utf8` COLLATE `utf8_bin`;
And: 和:
# Table: 'phpbb_forums_track'
CREATE TABLE phpbb_forums_track (
user_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
forum_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
mark_time int(11) UNSIGNED DEFAULT '0' NOT NULL,
PRIMARY KEY (user_id, forum_id)
) CHARACTER SET `utf8` COLLATE `utf8_bin`;
Then I look here in their wiki : 然后我在他们的wiki中看到这里 :
This table keeps record for visited topics in order to mark them as read or unread.
此表保留已访问主题的记录,以便将其标记为已读或未读。 We use the mark_time timestamp in conjunction with last post of topic x's timestamp to know if topic x is read or not.
我们将mark_time时间戳与主题x的时间戳的最后一个帖子结合使用,以了解是否读取了主题x。
In order to accurately tell whether a topic is read, one has to also check phpbb_forums_track.
为了准确地判断是否读取了主题,还必须检查phpbb_forums_track。
So essentially they have a lookup table to store the data associated with a user's viewing of a topic (thread), and then check it against the timestamp in the forum view table, to determine whether the topic has been viewed by the user. 因此,基本上他们有一个查找表来存储与用户查看主题(线程)相关联的数据,然后根据论坛视图表中的时间戳检查它,以确定用户是否已查看该主题。
Just create a simple cross-reference table ( read_posts
or something): 只需创建一个简单的交叉引用表(
read_posts
或其他):
user_id|post_id
----------------
2 | 132
53 | 43
....
Make sure that both of these columns are indexed (especially important that the user_id
be indexed) and then use a join (or a sub-query) to select unread posts for the logged in user. 确保对这两列都编制索引(特别重要的是将
user_id
编入索引),然后使用连接(或子查询)为登录用户选择未读帖子。 If you're just trying to show a list of unread posts, for example, you just run: 例如,如果您只是想显示未读帖子的列表,则只需运行:
SELECT * FROM `posts` WHERE `post_id` NOT IN (
SELECT `post_id` FROM `read_posts` WHERE `user_id`='[$USER ID]')
ORDER BY [your ordering clause]
Based on this description I would use a simple table with maybe 3 columns. 基于此描述,我将使用一个简单的表,可能有3列。
When a user views a post, add a row to the table. 当用户查看帖子时,向表中添加一行。 If a row does not exist in the table for a given user/post id combo, then they have not viewed the post.
如果表中不存在给定用户/帖子ID组合的行,则他们没有查看帖子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.