【问题标题】:does sequence of where condition changes the result in sql serverwhere 条件的顺序会改变 sql server 中的结果
【发布时间】:2013-12-03 10:25:29
【问题描述】:

我在一个查询中遇到了一个独特的问题。

insert into #tempclasssearch
            SELECT cc.Id, B.ScheduleId, 10, Lc.Id LocationId, Lc.GroupId, cc.CategoryId,CCSC.SubCategoryId,cc.RegistrationId
            from Event CC Inner join Batches B
            On Cc.Id = B.ClassCreativeId
            Inner Join Venues V On B.VenueId = V.ID
            Inner Join loc Lc On V.LocId = Lc.id
            Inner join EventSubCategories CCSC on CC.Id = CCSC.EventId
            inner join PackageSubscriptions p on p.userId = cc.userId
            Where 
            p.SubscriptionStatus = 1 and 
            ((@GroupId = 0) or (@GroupId is not null and Lc.GroupId = @GroupId))
            and ((@LocationId = 0) or (@LocationId is not null and Lc.Id = @LocationId))
            and ((@TargetCityId is null) or (@TargetCityId is not null and Lc.TargetCityId = @TargetCityId))                        
            and ((@CategoryId = 0) or (@CategoryId is not null and cc.CategoryId = @CategoryId))
            and ((@SubCategoryId = 0) or (@SubCategoryId is not null and ccsc.SubCategoryId = @SubCategoryId))
            and cc.UserStatus = 1 and cc.PublishStatus = 3 and B.StartDate >= @StartDateTime -- and B.StartDateTime > @StartDateTime
            and ((@EndDateTime is null) or (@EndDateTime is not null and B.StartDate <= @EndDateTime
            and p.StartDateTimeUtc <= GETUTCDATE() 
            and p.EndDateTimeUtc >= GETUTCDATE()

问题是,如果我将这个条件 p.SubscriptionStatus = 1 放在最后,那么它不会执行这个条件,而如果我把它作为第一个条件它会执行。

我不知道发生了什么:(

如果你们有任何想法,请告诉我......

【问题讨论】:

  • 最后第三行,即和 ((@EndDateTime is null) 或 (@EndDateTime is not null and B.StartDate

标签: sql sql-server join temp-tables


【解决方案1】:

最后没有括号

and ((@EndDateTime is null) or (@EndDateTime is not null and B.StartDate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2011-03-10
    相关资源
    最近更新 更多