SQL 问题 - 我的 WHERE 子句有什么问题

SQL Question - What is wrong with my WHERE clause

自从我不得不使用 SQL 并且不得不编写查询以来已经有几年了 – 但我得到了我意想不到的结果。我花了很多时间查看它,我确定我遗漏了一些明显的东西,但是,当我在下面 运行 时,我的查询返回 RateCode 值而不是我的 WHERE声明。

我做错了什么?我觉得这是一个足够基本的查询(我开始恐慌我已经失去了我的技能)

SELECT
    cs.Acct AS Site,
    cs.Addr1 AS Address,
    cs.City,
    cs.State,
    m.TypeDescr AS Service,
    se.Quantity,
    se.SvcCode,
    se.SvcDescr,
    sf.FreqDescr AS Frequency,
    se.SvcId, 
    se.StartDate,
    se.EndDate,
    sr.ReasonDescr AS Reason,
    svr.RateCode,  
    svr.StartDate AS RateStartDate,
    svr.EndDate AS RateEndDate

FROM
    wiSvcEquipment AS se
    LEFT JOIN wiCustSite    AS cs  ON cs.SiteId    = se.SiteId
    LEFT JOIN wiMaterial    AS m   ON m.MatSubType = se.MatSubType
    LEFT JOIN wiSvcReason   AS sr  ON sr.ReasonID  = se.ReasonID
    LEFT JOIN wiSvcVendRate AS svr ON svr.SvcId    = se.SvcID
    LEFT JOIN wiSvcCustRate as scr ON scr.RateCode = se.RateCode
    LEFT JOIN wiSvcFreq     AS sf  ON sf.FreqID    = se.FreqID

WHERE
    se.CustId = ‘013714’
    OR
    se.CustId = ‘014831’
    AND 
    svr.RateCode IN (
        ‘CHGCMBHAUL’, ‘CHGHAUFLAT’, ‘CHGHAUL2’, ‘CHGHAUL3’,
        ‘CHGHAULCOM’, ‘CHGHAULFE’, ‘CHGHAULING’, ‘CHGHAULMIN’,
        ‘CHGVCMBHAU’, ‘CHGVHAUFLT’, ‘CHGVHAULFE’, ‘CHGVHAULNG’,
        ‘CHGWKDHAUL’, ‘CLIHAUFLAT’, ‘CLIHAULFE’, ‘CLIHAULING’
    ) ;

将您的 WHERE 更改为对两列都使用 IN

WHERE
    se.CustId IN ( '013714', '014831' )
    AND 
    svr.RateCode IN (
        'CHGCMBHAUL', 'CHGHAUFLAT', 'CHGHAUL2', 'CHGHAUL3',
        'CHGHAULCOM', 'CHGHAULFE', 'CHGHAULING', 'CHGHAULMIN',
        'CHGVCMBHAU', 'CHGVHAUFLT', 'CHGVHAULFE', 'CHGVHAULNG',
        'CHGWKDHAUL', 'CLIHAUFLAT', 'CLIHAULFE', 'CLIHAULING'
    );

或者使用明确的逻辑组:

WHERE
    (
        se.CustId = '013714'
        OR
        se.CustId = '014831'
    )
    AND 
    svr.RateCode IN (
        'CHGCMBHAUL', 'CHGHAUFLAT', 'CHGHAUL2', 'CHGHAUL3',
        'CHGHAULCOM', 'CHGHAULFE', 'CHGHAULING', 'CHGHAULMIN',
        'CHGVCMBHAU', 'CHGVHAUFLT', 'CHGVHAULFE', 'CHGVHAULNG',
        'CHGWKDHAUL', 'CLIHAUFLAT', 'CLIHAULFE', 'CLIHAULING'
    );

以上两个子句完全相同:在SQL中,x IN ( a, b, c )运算符只是一系列( x = a OR x = b OR x = c )比较的语法糖。

你是说

(se.CustId = ‘013714’ OR se.CustId = ‘014831’)

AND 优先于 OR

我可以看到三件事:

  1. 第一个条件需要括号 - 或者更好 - IN
  2. 一系列 LEFT JOIN 中除了第一个 table 之外的所有条件都应该在 ON 子句中。
  3. 您的字符串有弯引号,但它们应该是“直”单引号。

所以:

SELECT . . .
FROM wiSvcEquipment se
     wiCustSite cs 
     ON cs.SiteId = se.SiteId LEFT JOIN
     wiMaterial m  
     ON m.MatSubType = se.MatSubType LEFT JOIN
     wiSvcReason sr 
     ON sr.ReasonID = se.ReasonID AND
            svr.RateCode IN ('CHGCMBHAUL', 'CHGHAUFLAT', 'CHGHAUL2', 'CHGHAUL3',
                             'CHGHAULCOM', 'CHGHAULFE', 'CHGHAULING', 'CHGHAULMIN’,
                             'CHGVCMBHAU', 'CHGVHAUFLT', 'CHGVHAULFE', 'CHGVHAULNG',
                             'CHGWKDHAUL', 'CLIHAUFLA’, 'CLIHAULFE', 'CLIHAULING'
                            ) LEFT JOIN

        LEFT JOIN
     wiSvcVendRate svr
     ON svr.SvcId = se.SvcID LEFT JOIN
     wiSvcCustRate scr
     ON scr.RateCode = se.RateCode LEFT JOIN
     wiSvcFreq sf 
     ON sf.FreqID = se.FreqID
WHERE se.CustId IN ('013714', '014831')

if se.CustId = '013714' 子句满足则无论 retcode 是什么,此查询都将 return 行。

您打算使用以下条件:

WHERE
    (se.CustId = ‘013714’
    OR
    se.CustId = ‘014831’)
    AND 
    svr.RateCode IN (
        ‘CHGCMBHAUL’, ‘CHGHAUFLAT’, ‘CHGHAUL2’, ‘CHGHAUL3’,
        ‘CHGHAULCOM’, ‘CHGHAULFE’, ‘CHGHAULING’, ‘CHGHAULMIN’,
        ‘CHGVCMBHAU’, ‘CHGVHAUFLT’, ‘CHGVHAULFE’, ‘CHGVHAULNG’,
        ‘CHGWKDHAUL’, ‘CLIHAUFLAT’, ‘CLIHAULFE’, ‘CLIHAULING’
    ) ;