单个表用于用户活动或单个选择查询

时间:2011-05-25 04:08:30

标签: mysql social-networking

我正在为我的客户创建一个社交网络,他们需要像Facebook一样的墙和用户最近的活动。

我有大约8个表说用户帐号,信息,群组,评论,页面,照片库,喜欢,分享。

user profile中,他们需要网站中用户的整个活动。

我可以使用一个加入查询,还是需要将个人表作为recentactivity

2 个答案:

答案 0 :(得分:1)

您可以使用联合查询从不同的表中提取数据并存储在视图中。这是一般的想法...

create view 'v_recent_activity' as 
select user_id, 'comment' as action, comment_text as action_info, comment_date as action_date from comments
union
select user_id, 'photo' as action, photo_url as action_info, photo_date as action_date from photos
union
select user_id, 'page' as action, page_url as action_info, comment_date as action_date from pages
union
select user_id, 'likes' as action, like_info as action_info, comment_date as action_date from likes
union
select user_id, 'shares' as action, share_info as action_info, share_date as action_date from shares

然后你可以用

之类的东西来访问它
Select * from v_recent_activity where action_date between <begin_date> and <end_date>

或加入

Select a.*, b.* from users a join v_recent_activity b on a.id = b.user_id where a.id = <user_id> and b.action_date between <begin_date> and <end_date>

答案 1 :(得分:0)

您可以考虑创建一个logactivity表,其中包含最近活动的记录。这样您只需要一个查询。您可以根据活动类型加入其他表。