我有以下代码:
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_title
或policy
guideline
并且没有sd_doc_classification
'%trust wide pol%'
的任何内容。
答案 0 :(得分:3)
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\%')
答案 1 :(得分:2)
正如Gordon和dnoeth所指出的那样,你的括号是个问题。我认为你实际上需要以下查询。
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和AND子句,使它们更容易可视化和理解。