SQL选择朋友更改列名称

时间:2012-05-20 12:05:49

标签: sql sql-server

我尝试在MS SQL上选择带有MemberID的好友列表,这里是我的代码

    SELECT MemberID, FriendID,(Select Username From Members Where MemberID = Friends.FriendID) as Username FROM Friends WHERE MemberID = @MemberID
    UNION
    SELECT MemberID as FriendID, FriendID as MemberID,(Select Username From Members Where MemberID = Friends.MemberID) as Username FROM Friends WHERE FriendID = @MemberID

,结果是

MemberID    FriendID  
1           2
1           7
34          1

如何将最后一行更改为

MemberID    FriendID  
1           2
1           7
1           34

谢谢!

1 个答案:

答案 0 :(得分:1)

SELECT  MemberID, FriendID
FROM    Friends
WHERE   MemberID = @MemberID
UNION ALL
SELECT  FriendID, MemberID
FROM    Friends
WHERE   FriendID = @MemberID