How to structure database for unread item alerts per user

前端 未结 3 879
闹比i
闹比i 2021-02-06 05:33

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 em

相关标签:
3条回答
  • 2021-02-06 05:43

    Based on this description I would use a simple table with maybe 3 columns.

    1. User ID
    2. Post ID
    3. Timestamp First Viewed

    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.

    0 讨论(0)
  • 2021-02-06 05:47

    While reviewing the relevant schema for phpBB, I found the following:

    # 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:

    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.

    In order to accurately tell whether a topic is read, one has to also check 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.

    0 讨论(0)
  • 2021-02-06 05:54

    Just create a simple cross-reference table (read_posts or something):

    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. 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]
    
    0 讨论(0)
提交回复
热议问题