社交网络数据库设计 - 朋友/区块关系

发布于 2024-12-07 05:35:02 字数 1467 浏览 0 评论 0原文

我正在开发一个社交网站,需要用户能够互相加好友和/或互相屏蔽。在我看来,2 个用户可以是朋友待定阻止NULL。我想要一个单一视图,为每个已确认的关系显示一行。我的视图正确显示了关系,但我必须采取解决方法,仅显示 1 行/关系,而不将表与其自身合并并交换顺序或请求者和被请求者。

有人对如何清理这个有任何想法吗?

谢谢, - 格雷格

关系表:

请求者(int)|被请求者 (int) |批准时间戳(小日期时间)| IsBlock(位)

vwRelationship 视图:

SELECT DISTINCT 
                      CASE WHEN f.Requestor < f.Requestee THEN f.Requestor ELSE f.Requestee END AS UserA, 
                      CASE WHEN f.Requestor < f.Requestee THEN f.Requestee ELSE f.Requestor END AS UserB, CASE WHEN b.Requestor IS NULL AND b.Requestee IS NULL 
                      THEN CASE WHEN f.AcceptedTimestamp IS NULL THEN 'Pending' ELSE 'Friend' END ELSE 'Block' END AS Type
FROM         dbo.Relationship AS f LEFT OUTER JOIN
                          (SELECT     Requestor, Requestee
                            FROM          dbo.Relationship
                            WHERE      (IsBlock = 1)) AS b ON f.Requestor = b.Requestor AND f.Requestee = b.Requestee OR f.Requestor = b.Requestee AND f.Requestee = b.Requestor

示例查询:

从 vwRelationship 选择类型,其中 (UserA = 1 AND UserB = 2) OR (UserA = 2 AND UserB = 1)

场景:

  1. 用户 1 和用户 2 彼此不认识 |关系类型 = NULL
  2. 用户 1 朋友 用户 2 |关系类型 = 待处理
  3. 用户 2 接受 |关系类型 =
  4. 一个月后朋友 用户 2 屏蔽用户 1 |关系类型 = 阻止

I'm working on a social networking site and need users to be able to friend each other and/or block each other. The way I see it, 2 users can either be Friend, Pending, Block, or NULL. I'd like to have a single view that shows a single row for each confirmed relationship. My view properly shows the relationship but I had to do a workaround to only show 1 row/relationship without unioning the table with itself and swapping the order or Requestor and Requestee.

Anybody have any ideas about how to clean this up?

Thanks,
- Greg

Relationship Table:

Requestor (int) | Requestee (int) | ApprovedTimestamp (smalldatetime) | IsBlock (bit)

vwRelationship View:

SELECT DISTINCT 
                      CASE WHEN f.Requestor < f.Requestee THEN f.Requestor ELSE f.Requestee END AS UserA, 
                      CASE WHEN f.Requestor < f.Requestee THEN f.Requestee ELSE f.Requestor END AS UserB, CASE WHEN b.Requestor IS NULL AND b.Requestee IS NULL 
                      THEN CASE WHEN f.AcceptedTimestamp IS NULL THEN 'Pending' ELSE 'Friend' END ELSE 'Block' END AS Type
FROM         dbo.Relationship AS f LEFT OUTER JOIN
                          (SELECT     Requestor, Requestee
                            FROM          dbo.Relationship
                            WHERE      (IsBlock = 1)) AS b ON f.Requestor = b.Requestor AND f.Requestee = b.Requestee OR f.Requestor = b.Requestee AND f.Requestee = b.Requestor

Example Query:

Select Type From vwRelationship Where (UserA = 1 AND UserB = 2) OR (UserA = 2 AND UserB = 1)

Scenario:

  1. User 1 and User 2 don't know each other | Relationship Type = NULL
  2. User 1 friends User 2 | Relationship Type = Pending
  3. User 2 accepts | Relationship Type = Friend
  4. a month later User 2 blocks User 1 | Relationship Type = Block

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2024-12-14 05:35:02

这就是我最终使用的:

Table - Relationship
RelationshipID, RelationshipTypeID, CreatedByUserID, CreatedTimestamp

Table - RelationshipType
RelationshipTypeID, RelationshipTypeName

Table - UserRelationship
UserID, RelationshipID, IsPending

有人想到更好的吗?

Here's what I ended up using:

Table - Relationship
RelationshipID, RelationshipTypeID, CreatedByUserID, CreatedTimestamp

Table - RelationshipType
RelationshipTypeID, RelationshipTypeName

Table - UserRelationship
UserID, RelationshipID, IsPending

Anybody think of anything better?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文