我想建立一些类似于 facebook 的事件提要。在我提出问题之前,我想解释一下我的 mySQL 数据库设计。我有一个用户表,即。
user_id name other columns
userA - -
userB - -
userC - -
userD - -
用户可以互相关注,这里是名为“关注者”的表
id user_id follower_id
1 userA userB
2 userD userB
3 userB userA
-
-
-
如我们所见,在上表中,用户 B 跟随用户 A 和用户 D。
现在我的问题是,当 userB 所关注的 user_id 中的任何用户进行事件时,都应该得到通知。我应该制作另一张 table ,让我们说“事件”,例如:
user_id activity_type activity_link activity_date
userA note
userB photo
userC note
userD photo
问题 1:我是否需要制作额外的表格(如上所示)来存储事件,还是可以不用它?
问题 2:由于 userB 只关注 userA 和 userD,因此 userB 只关心他关注的用户,我如何为此构建 sql 查询?某某:
Select * from activity where user_id exists in user_id in followers table where follower_id = userB
(这是我的想法,你能帮我构建 SQL 查询吗)
感谢您阅读这个长问题和可能的帮助。
最佳答案
是的,您需要一个表来存储事件数据。否则,您将不得不将这些数据存储在其他地方。
编写查询的最简单方法是使用连接。在这种情况下,如果当前用户正在关注,我们只需要事件行,因此我们可以使用内部联接。
select * from activity a
join followers f on a.user_id = f.user_id
where follower_id = 'userB'
关于sql - 事件源的数据库设计和 SQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4905767/