通知警报关闭sql php

问题描述 投票:0回答:1

我正在创建一个基本的通知系统来提醒用户,他所关注的用户创建了一个新帖子。

用户

  id_user | name
    1       Max
    2       Joe
    3       Ed
    4       Tommy

帖子

    id_post | id_user_post | posts
    1               2           hi
    2               2           hello
    3               2           how are you
    4               3           hey you
    5               2           how long
    6               1           whats up
    7               2           come on

社区

  id_follower id_followed
    3           2
    3           1
    4           2

在这种情况下,Ed(用户3)跟随Joe(2)和Max(1),他们俩都发布了6个帖子。

  SELECT COUNT(*)
    FROM community c
     LEFT JOIN posts p ON p.id_user_post=c.id_followed
   WHERE c.id_follower=3 

这是它在页面中的样子

  Homepage header
   You have (6 new posts) > [click here to see]

我的问题是如何在点击后关闭通知提醒(6个新帖)?我需要创建通知表吗?我是否需要在帖子中添加状态字段?我是否需要再次进行Sql查询?否则该通知将永远显示。

php sql left-join
1个回答
0
投票

您应该在last_post_id表中添加community列。然后,您只能计算ID高于此值的帖子。

  SELECT COUNT(*)
    FROM community c
     LEFT JOIN posts p ON p.id_user_post=c.id_followed AND p.id_post > c.last_post_id
   WHERE c.id_follower=3 

每当您向用户显示状态时,都会将last_post_id更新为最高ID:

UPDATE community AS c
JOIN (SELECT id_user_post, MAX(id_post) AS id_post
      FROM posts
      GROUP BY id_user_post) AS p ON p.id_user_post=c.id_followed
SET c.last_post_id = p.id_post
WHERE c.id_follower = 3
© www.soinside.com 2019 - 2024. All rights reserved.