我有以下代码:

SELECT sdd.sd_doc_classification,
       sdd.sd_title,
       sdd.sd_desc,
       sdr.sd_upload_fname
FROM sdoc_docs AS sdd
INNER JOIN sdoc_revh AS sdr ON sdd.sd_id = sdr.sd_id
WHERE (sdd.sd_title LIKE '%policy%')
    OR (sdd.sd_title LIKE '%guideline%')
    AND (sdd.sd_doc_classification NOT LIKE '%\Trust Wide Policies & Guidelines\%')

但是,添加“OR (sdd.sd_title LIKE '%guideline%')”会破坏它,现在该语句返回的值类似于'%\Trust Wide Policies & Guidelines\%'
删除上面的指导示例后,代码将按预期运行。
本质上,我想显示任何具有sd_titlelikepolicyguideline且不具有sd_doc_classificationlike'%trust wide pol%'的内容。

最佳答案

AND具有比OR更高的优先级-即,就好像您编写了这样的查询:

SELECT sdd.sd_doc_classification,
       sdd.sd_title,
       sdd.sd_desc,
       sdr.sd_upload_fname
FROM       sdoc_docs AS sdd
INNER JOIN sdoc_revh AS sdr ON sdd.sd_id = sdr.sd_id
WHERE      (sdd.sd_title LIKE '%policy%') OR
           ((sdd.sd_title LIKE '%guideline%') AND
            (sdd.sd_doc_classification NOT LIKE
             '%\Trust Wide Policies & Guidelines\%'))

如果要控制计算条件的优先级,可以自己显式添加括号:
SELECT sdd.sd_doc_classification,
       sdd.sd_title,
       sdd.sd_desc,
       sdr.sd_upload_fname
FROM       sdoc_docs AS sdd
INNER JOIN sdoc_revh AS sdr ON sdd.sd_id = sdr.sd_id
WHERE      ((sdd.sd_title LIKE '%policy%') OR
            (sdd.sd_title LIKE '%guideline%')) AND
           (sdd.sd_doc_classification NOT LIKE
             '%\Trust Wide Policies & Guidelines\%')

08-07 10:44