Sql 通过多个Where条件简化Select查询

Sql 通过多个Where条件简化Select查询,sql,sql-server,select,where,Sql,Sql Server,Select,Where,我在SQL Server 2012上选择了查询,带有多个where条件,下面是我的代码: SELECT * FROM ##TempGet a LEFT JOIN Techs b ON a.TECH = b.TechID --LEFT JOIN (SELECT * FROM OPENQUERY (Serv, 'SELECT * FROM Hist order by histdate desc')) as c on a.CUST_ACCT = c.CUST_ACCT

我在SQL Server 2012上选择了查询,带有多个where条件,下面是我的代码:

   SELECT *
    FROM ##TempGet a
    LEFT JOIN Techs b ON a.TECH = b.TechID
    --LEFT JOIN (SELECT * FROM OPENQUERY (Serv,  'SELECT * FROM Hist order by histdate desc')) as c on a.CUST_ACCT = c.CUST_ACCT and c.HISTDATE >= a.sch_date
    LEFT JOIN (SELECT ParamId, ParamType, Value1, Value2, Value3, Status, UpdateBy, UpdateDate FROM Parameter AS Parameter_3 WHERE (ParamType = 'SCABranch')) AS p ON LEFT (SCHAREAS, 1) = p.Value1
WHERE WSTAT = 'P/TC' 
AND (a.CUST_ACCT NOT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM NOT IN (SELECT OFFERNUM from TC))
OR (a.CUST_ACCT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM  IN (SELECT OFFERNUM from TC ) and a.sch_date not in (SELECT SCH_DATE from TC where CUST_ACCT = a.CUST_ACCT and OFFERNUM = a.Offernum))
OR (a.CUST_ACCT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM NOT IN (SELECT OFFERNUM from TC ) and a.sch_date not in (SELECT SCH_DATE from TC where CUST_ACCT = a.CUST_ACCT and OFFERNUM = a.Offernum))
最主要的问题是,情况太复杂

 WHERE WSTAT = 'P/TC' 
    AND (a.CUST_ACCT NOT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM NOT IN (SELECT OFFERNUM from TC))
    OR (a.CUST_ACCT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM  IN (SELECT OFFERNUM from TC ) and a.sch_date not in (SELECT SCH_DATE from TC where CUST_ACCT = a.CUST_ACCT and OFFERNUM = a.Offernum))
    OR (a.CUST_ACCT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM NOT IN (SELECT OFFERNUM from TC ) and a.sch_date not in (SELECT SCH_DATE from TC where CUST_ACCT = a.CUST_ACCT and OFFERNUM = a.Offernum))
这是真的吗?因为我选择的某些数据不显示,但应该显示在那里。

你的括号对吗?在你的三行中,你打算“还是”这三行?例如,如果WSTAT='P/TC'应仅应用下三行中的一行吗?如果是这样的话,试着用括号括起来

    WHERE WSTAT = 'P/TC' 
        AND  (  -- added bracket
           (a.CUST_ACCT NOT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM NOT IN (SELECT OFFERNUM from TC))
        OR (a.CUST_ACCT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM  IN (SELECT OFFERNUM from TC ) and a.sch_date not in (SELECT SCH_DATE from TC where CUST_ACCT = a.CUST_ACCT and OFFERNUM = a.Offernum))
        OR (a.CUST_ACCT IN (SELECT CUST_ACCT FROM TC) AND a.OFFERNUM NOT IN (SELECT OFFERNUM from TC ) and a.sch_date not in (SELECT SCH_DATE from TC where CUST_ACCT = a.CUST_ACCT and OFFERNUM = a.Offernum))  
             ) -- added bracket
这一切都归结为谓词。简单地说,谓词是一个事实语句,通常位于查询的where部分可以在其他位置执行—在内部/外部join HAVING子句的ON部分。每个谓词都必须为真,并且只有当每个谓词为真时,才能得到结果。那么你是在问

        Get me <the columns I've listed>
    from <the tables I've listed>
    where 
      predicate 1 -- something you've written
      predicate 2 -- WHERE WSTAT = 'P/TC' 
      predicate 3 -- the AND line
      predicate 4 -- the OR line
      predicate 4 -- the other OR 
我猜你想

        Get me <the columns I've listed>
    from <the tables I've listed>
    where 
      predicate 1 -- something you've written
      predicate 2 -- WHERE WSTAT = 'P/TC' 
      predicate 3 -- the AND line or the first OR or the 2nd OR


对于像这样的查询逻辑问题,有时注释掉查询的一部分(几乎总是where子句)有助于识别阻止返回预期数据的原因。然后深入查看相关部分。

在语句a、语句b或语句c后面加括号,这意味着查询将选择其中一个,对吗?是的,我想要的是WSTAT='p/TC',并选择一个适合于或station的条件