T-Sql Query:如何在一条记录中满足两个条件而不是一条记录

时间:2012-01-24 09:33:51

标签: sql-server tsql

我查询了以下查询:

select Opp.[ID], Opp.ContactID, Opp.RTSID, Opp.[ProjectName],
       CONVERT(varchar, Opp.[PurchaseDate], 103) as PurchaseDate,
       CONVERT(varchar,Opp.[SubmissionDate],103) as SubmissionDate,
       Opp.[Cost], Opp.[Selling], Opp.[CompanyName], Opp.[Telephone], 
       Opp.[Fax], Con.[ContactName], Con.[ContactPosition], 
       Con.[ContactDirectPhone], Con.[ContactMobile], Con.[ContactEmail],
       lookup.DescriptionEn as ProjectStatus, Opp.[Wining], Opp.[Remark],
       Opp.[CustomerReference], Sales.FName + ' ' + Sales.LName as SalesMan
from Opportunities Opp inner join Sales on Opp.Sales_Num = Sales.Sales_Num
     left join
       (select MINOR, DescriptionEn from lookup_table where MAJOR = '1') lookup
       ON lookup.MINOR = Opp.ProjectStatus
     left join
       (select ID, ContactName, ContactPosition, ContactDirectPhone, ContactMobile,
                   ContactEmail from Contacts) Con ON Con.ID = Opp.ContactID
where (lookup.DescriptionEn = @ProjectStatus or @ProjectStatus ='-1') and 
      (Opp.Sales_Num = @SalesMan or @SalesMan ='-1') and
      (Opp.Selling >= @Selling or @Selling ='0') and 
      (lookup.MINOR <> '7' and datediff(day , Opp.SubmissionDate , getdate()) < 30 )
End

最后一行同意条件和(lookup.MINOR&lt;&gt;'7'或.......) 没有按预期工作它会获得MINOR&lt;&gt;的记录'7'或其他

我想指定仅在一个记录中遇到的 两个条件

怎么做?

2 个答案:

答案 0 :(得分:2)

替换

(lookup.MINOR <> '7' and datediff(day , Opp.SubmissionDate , getdate()) < 30 )

通过

(lookup.MINOR <> '7' OR datediff(day , Opp.SubmissionDate , getdate()) < 30 )

答案 1 :(得分:2)

使用 AND 代替 OR