2011-07-17 84 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

太棒了!你能将答案标记为正确吗?! –