gpt4 book ai didi

SQL - 组合 AND & OR

转载 作者:搜寻专家 更新时间:2023-10-30 20:04:13 31 4
gpt4 key购买 nike

我有以下代码:

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 政策和指南\%'

删除上面的指南示例后,代码按预期运行。

基本上我想显示任何有 sd_title 的东西,比如 policyguideline 而没有 sd_doc_classification 喜欢 '%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\%')

关于SQL - 组合 AND & OR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34676438/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com