我需要帮助来创建SQL查询

时间:2011-07-17 22:34:34

标签: sql-server

我正在使用asp.net创建一个个人网站,但我对SQL并不熟悉,在创建查询时需要帮助。

我有3个表格,其中包含以下列:

  

UserInfos - UserID,NickName,PictureAva和Privacy   UserFollower - FollowerId,FollowingId,Status。
  PlaceFollower - FollowerId,FollowingId,Date,Status。

我需要从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 个答案:

答案 0 :(得分:1)

我会建议UNION。

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