如何向用户和用户本身的朋友显示用户数据

时间:2019-01-16 10:04:17

标签: database ssms inner-join

仅当其他用户 User1 的朋友时,我才需要向其他用户显示 User1 的数据。此外,我需要将数据显示给User1本身。我正在通过 NurseFriend 表上的内部联接尝试此操作,在该表中我编写的查询仅显示 NurseId 等于 ArticleId 的用户数据>并且接受好友请求。现在,即使他/她没有朋友,我也需要向 user1 显示数据。

使用以下代码对其进行了尝试:

SELECT    A.[ArticleId]     
        , A.[NurseId]       
        , A.[NurseName] 
        , A.[LastName]
        , A.[ProfileImage]  
        , A.[Title]         
        , A.[Description]   
        , A.[CreatedOn] 
        , A.[Images]
        , A.[videoURL]  
        , A.hasLiked
        , A.hasShared
        , A.prev_des
        , A.prev_img
        , A.prev_Title
        , A.Url_Desc

        , [CommentCount]    = ISNULL(Sum(CommentCount),0)
        , [LikeCount]       = ISNULL(SUM([LikeCount]    ),0)    
        , [ViewCount]       = ISNULL(SUM([ViewCount]    ),0)    
        , [SharedCount]     = ISNULL(SUM([SharedCount]  ),0)

FROM    #Article A
        Inner Join #ArticleSocialActivity ASA on A.ArticleId = ASA.[ArticleId]
        inner join NurseFriend NF with(nolock) on NF.NurseId = A.NurseId or NF.FNurseId = A.NurseId and NF.IsAccepted = 1
                where A.[NurseId] = @NurseId or NF.NurseId = @NurseId or NF.FNurseId = @NurseId



GROUP BY 
        A.ArticleId
        , A.[NurseId]       
        , A.[NurseName]
        , A.[LastName]  
        , A.[ProfileImage]  
        , A.[Title]         
        , A.[Description]   
        , A.[CreatedOn]
        , A.[Images]
        , A.[videoURL]  
        , A.[hasLiked]  
        , A.[hasShared]
        , A.[prev_des]
        , A.[prev_img]
        , A.[prev_Title]
        , A.[Url_Desc]


        ORDER BY CreatedOn desc

0 个答案:

没有答案