0

我正在开发一个 asp.net 应用程序“社交网站”。在此我正在处理以下表格:-

Friends(ID,User_ID,Friend_ID)
Users(User_ID,User_Name,Full_Name,DOB,Gender)
User_Detail(ID,User_ID,Image,Father_Name,Mother_Name,Location,JobProfile)
Wall(Wall_ID,Wall_Content,TimeAgo,User_ID)
Wall_Comments(Comment_ID,Wall_ID,Comments,Comment_Date,User_ID)

我想选择 User_ID=5 的 Tables- Wall 和 Wall_Comments 的所有记录以及 User_ID=5 的所有朋友我已经完成了以下 Sql 查询:

SELECT DISTINCT TOP (30) wall.wall_content, 
                         wall.wall_id, 
                         wall.user_id, 
                         wall.timeago, 
                         users.user_name, 
                         users.full_name, 
                         users.user_id                                 AS UID, 
                         wall_comments.comments, 
                         wall_comments.comment_id, 
                         CONVERT(NVARCHAR(1000), user_detail.image, 0) AS Image 
FROM   friends 
       INNER JOIN users 
               ON friends.user_id = users.user_id 
       INNER JOIN wall 
               ON users.user_id = wall.user_id 
       INNER JOIN user_detail 
               ON users.user_id = user_detail.user_id 
       left outer join wall_comments 
               ON wall.wall_id = wall_comments.wall_id 
WHERE  ( users.user_id IN (SELECT friend_id 
                           FROM   friends AS Friends_1 
                           WHERE  ( user_id = 5 )) ) 
UNION 
SELECT wall.wall_content, 
       wall.wall_id, 
       wall.user_id, 
       wall.timeago, 
       users.user_name, 
       users.full_name, 
       users.user_id                                 AS UID, 
       wall_comments.comments, 
       wall_comments.comment_id, 
       CONVERT(NVARCHAR(1000), user_detail.image, 0) AS Image 
FROM   wall 
       INNER JOIN users 
               ON wall.user_id = users.user_id 
       INNER JOIN user_detail 
               ON users. user_id = user_detail.user_id 
       left outer join wall_comments 
               ON wall.wall_id = wall_comments.wall_id 
WHERE  ( users.user_id = 5 ) 
ORDER  BY wall.wall_id DESC 

但是它只显示了一条桌墙记录和 user_ID-5 的 Wall_Comments 它没有显示他的朋友 Wall 和他们的帖子。如果他们在任何墙上没有任何评论,那么它不会显示。请帮帮我。

4

1 回答 1

0

Something like this?

select *
from Friends
inner join Wall_Comments On (Wall_Comments.User_ID = Friends.User_ID or Wall_Comments.User_ID = Friends.Friend_ID)
inner join Users On Users.User_ID = Wall_Comments.User_ID
where Friends.User_ID = 5
于 2013-10-03T13:18:30.707 回答