如何选择当前用户参与的所有关系? ASP.NET SQL Server

时间:2018-06-02 22:17:59

标签: sql asp.net tsql

我有这个用户和关系的数据库。我只是想显示某些特定(当前登录用户)username所存在的所有关系的firstNamelastNameuserID。我该怎么做?

这是我最好的尝试。

SELECT 
    UserRelationship.relationshipType, 
    UserRelationship.userFirstID, UserRelationship.userSecondID, 
    [User].username, [User].firstName, [User].lastName
FROM  
    [User] 
INNER JOIN
    UserRelationship ON [User].userID = UserRelationship.userFirstID 
                     AND UserRelationship.relationshipType = 'friends' 
                     OR [User].userID = UserRelationship.userSecondID 
                     AND UserRelationship.relationshipType = 'friends'
WHERE 
    ([User].userID = @userID)        

这将返回所有关系,但返回当前用户的用户名,名字和姓氏。

这些是我的表格:

CREATE TABLE [dbo].[UserRelationship] 
(
    [userFirstID]      INT           NOT NULL,
    [userSecondID]     INT           NOT NULL,
    [initiatedBy]      NVARCHAR (50) NOT NULL,
    [relationshipType] NVARCHAR (50) NOT NULL,

    CONSTRAINT [PK_UserRelationship] 
        PRIMARY KEY CLUSTERED ([userFirstID] ASC, [userSecondID] ASC),
    CONSTRAINT [FK_UserRelationship_userFirstID] 
        FOREIGN KEY ([userFirstID]) REFERENCES [dbo].[User] ([userID]),
    CONSTRAINT [FK_UserRelationship_initiatedBy] 
        FOREIGN KEY ([initiatedBy]) REFERENCES [dbo].[User] ([username]),
    CONSTRAINT [CK_UserRelationship_relationshipType] 
        CHECK ([relationshipType]='pending' OR [relationshipType]='friends')
);

CREATE TABLE [dbo].[User] 
(
    [userID]      INT            IDENTITY (1, 1) NOT NULL,
    [username]    NVARCHAR (50)  NOT NULL,
    [firstName]   NVARCHAR (50)  NULL,
    [lastName]    NVARCHAR (50)  NULL,
    [dateOfBirth] DATE           NULL,
    [city]        NVARCHAR (50)  NULL,
    [address]     NVARCHAR (50)  NULL,
    [phoneNumber] INT            NULL,
    [email]       NVARCHAR (50)  NULL,
    [rank]        NVARCHAR (50)  NULL,
    [profilImage] NVARCHAR (255) NULL,

    PRIMARY KEY CLUSTERED ([userID] ASC),
    CONSTRAINT [AK_User_username] 
        UNIQUE NONCLUSTERED ([username] ASC)
);

在我的UserRelationship表中,我始终确保userFirstID低于userSecondID来存储所有关系。所以我每个关系只有1条记录。

1 个答案:

答案 0 :(得分:0)

您想获取当前用户与之友好关系的用户名吗?然后一种可能性是再次加入用户的表两次,对于关系表中的第一个和第二个id,并检查CASE ... END哪一个是另一个用户并返回他们的名字。

SELECT UserRelationship.relationshipType,
       UserRelationship.userFirstID,
       UserRelationship.userSecondID,
       [User].username,
       [User].firstName,
       [User].lastName,
       CASE [User].[UserID]
         WHEN OtherUser1.userId
           THEN OtherUser2.userName
         WHEN OtherUser2.userId
           THEN OtherUser1.userName
       END OtherUserUserName,
       CASE [User].[UserID]
         WHEN OtherUser1.userId
           THEN OtherUser2.firstName
         WHEN OtherUser2.userId
           THEN OtherUser1.firstName
       END OtherUserFirstName,
       CASE [User].[UserID]
         WHEN OtherUser1.userId
           THEN OtherUser2.lastName
         WHEN OtherUser2.userId
           THEN OtherUser1.lastName
       END OtherUserLastName
       FROM [User]
            INNER JOIN UserRelationship
                       ON [User].userID IN (UserRelationship.userFirstID,
                                            UserRelationship.userSecondID)
                          AND UserRelationship.relationshipType = 'friends' 
            INNER JOIN [User] OtherUser1
                       ON OtherUser1.userID = UserRelationship.userFirstID
            INNER JOIN [User] OtherUser2
                       ON OtherUser2.userID = UserRelationship.userSecondID
       WHERE ([User].userID = @userID);

(假设用户不能成为他们自己的朋友。如果所有三个ID相等,则需要扩展CASE .... END。)