How to structure database for unread item alerts per user

前端 未结 3 884
闹比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: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]
    

提交回复
热议问题