我有 2 张桌子,其中一张是“请求”,我有向其发送好友请求的用户,或者我收到了好友请求。在第二张表中,我为自己保留了入围用户的记录。现在,我想一起选择所有这些用户(我请求过的用户、请求过我的用户、我入围的用户)。我为此使用联合查询并根据列类型区分它们
select Distinct userid,type,status from
(
select RequestSenderId as UserId,'requestedme' as type,'1' as status from tblrequest where RequestReceiverId=@UserId
union
select RequestReceiverId as UserId,'requestedbyme' as type,'2' as status from tblrequest where RequestSenderId=@UserId
union
select Shortlisteduserid as UserId,'Shortlisted' as type,'0' as status from tblshortlist where userid=@UserId
)
问题是,如果我也将其列入候选名单,我将无法获得不同的用户 ID,并且他也向我发送了请求。
任何人都可以建议如何从结果中获得不同的用户 ID。优先级是按要求获取用户 ID,而不是入围。