我正在开发一个社交网站,需要用户能够互相交流和/或互相阻止。我看到它的方式,2个用户可以是朋友,待定,阻止或 NULL 。我希望有一个单独的视图,显示每个已确认关系的单行。我的观点正确地显示了这种关系,但我必须做一个解决方法,只显示1行/关系而不将表与自身联合并交换订单或请求者和请求者。
有人对如何清理它有任何想法吗?
谢谢, - 格雷格
关系表:
Requestor(int)|请求者(int)| ApprovedTimestamp(smalldatetime)| 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中选择Type(UserA = 1 AND UserB = 2)OR(UserA = 2 AND UserB = 1)
情景:
答案 0 :(得分:3)
以下是我最终使用的内容:
Table - Relationship
RelationshipID, RelationshipTypeID, CreatedByUserID, CreatedTimestamp
Table - RelationshipType
RelationshipTypeID, RelationshipTypeName
Table - UserRelationship
UserID, RelationshipID, IsPending
有人想到更好的事情吗?