我创建了一个基本的通知系统来提醒用户,他所关注的用户创建了一个新帖子。
用户
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查询?否则该通知将永远显示。
答案 0 :(得分: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