社交网络数据库设计 - 朋友/区块关系
我正在开发一个社交网站,需要用户能够互相加好友和/或互相屏蔽。在我看来,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 和用户 2 彼此不认识 |关系类型 = NULL
- 用户 1 朋友 用户 2 |关系类型 = 待处理
- 用户 2 接受 |关系类型 =
- 一个月后朋友 用户 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:
- User 1 and User 2 don't know each other | Relationship Type = NULL
- User 1 friends User 2 | Relationship Type = Pending
- User 2 accepts | Relationship Type = Friend
- a month later User 2 blocks User 1 | Relationship Type = Block
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我最终使用的:
有人想到更好的吗?
Here's what I ended up using:
Anybody think of anything better?