Sql server 当我有不同值的列时,如何获得不同的结果

Sql server 当我有不同值的列时,如何获得不同的结果,sql-server,sql-server-2005,Sql Server,Sql Server 2005,我有两个表,其中一个是“请求”,我有一些用户向他们发送了好友请求,或者我收到了好友请求。在第二个表中,我为自己保存了入围用户的记录。现在,我想选择所有这些用户,他们是我请求的,他们是我请求的,他们是我入围的。我正在为此使用联合查询,并根据列类型对它们进行区分 select Distinct userid,type,status from ( select RequestSenderId as UserId,'requestedme' as type,'1' as status from tblr

我有两个表,其中一个是“请求”,我有一些用户向他们发送了好友请求,或者我收到了好友请求。在第二个表中,我为自己保存了入围用户的记录。现在,我想选择所有这些用户,他们是我请求的,他们是我请求的,他们是我入围的。我正在为此使用联合查询,并根据列类型对它们进行区分

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。优先考虑的是按照请求获取用户ID,而不是入围名单。

这可能会对您有所帮助

select userid,type,status from
(
select Distint  RequestSenderId ,'requestedme' as type,'1' as status from tblrequest where ReceiverId=@UserId 

union ALL
select Distint  RequestReceiverId ,'requestedbyme' as type,'2' as status from tblrequest where SenderId=@UserId 

union ALL
select Distint  Shortlisteduserid ,'Shortlisted' as type,'0' as status from tblshortlist where userid=@UserId
)

下面呢?不过,这可能不是最有效的解决方案

WITH requestedMe (userid, type, status)
    AS (
        SELECT RequestSenderId AS UserId,'requestedme' AS type,'1' AS status 
        FROM tblrequest 
        WHERE ReceiverId=@UserId)
    requestedByMe (userid, type, status)
    AS (
        SELECT RequestReceiverId AS UserId,'requestedbyme' AS type,'2' AS status 
        FROM tblrequest 
        WHERE SenderId=@UserId
            AND NOT EXISTS (
                SELECT *
                FROM requestedMe
                WHERE requestedMe.userid = tblrequest.RequestReceiverId))
    shortListed (userid, type, status)
    AS (
        SELECT Shortlisteduserid AS UserId,'Shortlisted' AS type,'0' AS status 
        FROM tblshortlist 
        WHERE userid=@UserId
            AND NOT EXISTS (
                SELECT *
                FROM requestedMe
                WHERE requestedMe.userid = tblshortlist.Shortlisteduserid)
            AND NOT EXISTS (
                SELECT *
                FROM requestedByMe
                WHERE requestedByMe.userid = tblshortlist.Shortlisteduserid))
SELECT userid, type, status
FROM requestedMe
UNION
SELECT userid, type, status
FROM requestedByMe
UNION
SELECT userid, type, status
FROM shortListed

您可以使用排名函数行数或密集排名对结果进行优先级排序

SELECT UserId, Type, Status
FROM (
    SELECT UserId, Type, Status
    , Priority = ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY Type DESC)
    FROM (
        SELECT ReceiverId, RequestSenderId, 'requestedme', 1 FROM tblRequest
        UNION ALL
        SELECT SenderId, RequestReceiverId, 'requestedbyme', 2 FROM tblRequest
        UNION ALL
        SELECT UserId, Shortlisteduserid ,'Shortlisted', 0 FROM tblShortlist
    ) U (FilterUserId, UserId, Type, Status)
    WHERE FilterUserId = @UserId
) R
WHERE Priority = 1

你试过使用UNION ALL吗?是的。结果是一样的问题到底是什么?某个特定的用户ID在最终结果集中显示两次,而您只需要一次?您使用的是哪个版本的sql server?@arion我使用的是sql server 2005否,它会给出相同的结果,因为您不是从总结果中选择Destinc,而是从单独的结果中选择Destinctables@nav试着和大家一起,我发现它在oracle中使用了一些模拟数据