2011-07-17 130 views
1

我使用asp.net創建個人網站,但我不熟悉SQL,需要幫助創建查詢。我需要幫助來創建一個SQL查詢

我有3個表有以下欄目:

UserInfos - 用戶名,暱稱,PictureAva和隱私。
UserFollower - FollowerId,FollowingId,Status。
PlaceFollower - 關注者ID,以下ID,日期,狀態。

我需要從UserInfos表中選擇每個人。

標準:

If Privacy = 0, I don't want this Row to be returned. 
If Privacy = 1, I need that the rows from UserInfos be returned when 
       UserInfos.UserID = UserFollower.FollowingId 
      and UserFollower.FollowerId = 2 
      and UserFollower.Status = 1, 
      only if PlaceFollower.FollowerId = UserFollower.FollowingId 
       and PlaceFollower.FollowingId = 1 
       and PlaceFollower.Status = 1 
       and PlaceFollower.Date = '17/07/2011'. 

    If Privacy = 2, I need that the rows from UserInfos be returned when 
      UserInfos.UserID = UserFollower.FollowingId 
      and UserFollower.FollowerId = 2 
      and UserFollower.Status = 1, 
     **OR** UserInfos.UserID = UserFollower.FollowerId 
      and UserFollower.FollowingId = 2 
      and UserFollower.Status = 1, 
      only if PlaceFollower.FollowerId = UserFollower.FollowingId 
      and PlaceFollower.FollowingId = 1 
      and PlaceFollower.Status = 1 
      and PlaceFollower.Date = '17/07/2011'. 

是否有可能做到這一點?

感謝您的幫助。

回答

1

我會建議UNION。

select .. join .. where privacy=1 
UNION ALL 
select .. join .. where privacy=2 
+0

謝謝,工作! – Felipe

+0

太棒了!你能將答案標記爲正確嗎?! –