SQL SELECT 和 WHERE

huangapple go评论65阅读模式
英文:

SQL SELECT and WHERE

问题

我想选择PL类别52105下的交易和交易代码9007和9803,但结果选择了除这两个之外的其他交易代码。我错在哪里?

FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04] 
   
WHERE PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')  

order by BOOKING_DATE_STMT

SQL SELECT 和 WHERE

英文:

I Want to select transactions under PL Category 52105 and transaction codes 9007 & 9803, but the results are are picking other transaction codes other than those 2. Where am I getting it wrong?

FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04] 
   
WHERE PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')  

order by BOOKING_DATE_STMT

SQL SELECT 和 WHERE

答案1

得分: 1

这是优先级的顺序,使得OR语句后面的所有内容都被视为这样处理。

尝试以下方式,通过使用括号强制指定顺序:

WHERE (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL) AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')

请注意,在参与OR子句的两个语句周围有两个括号。

英文:

Its the order of precedence that yields everything behind the OR statement to be treated as just that.

Try the following instead, forcing the order by using brackets:

WHERE (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL) AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')

Note the two brackets around the two statements participating in the OR clause.

答案2

得分: 0

Consider rewriting like this:

FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04] 

WHERE TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R%')  AND (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL)

ORDER BY BOOKING_DATE_STMT

你可能需要检查你的 "not like" 如何编写,它应该包括通配符,具体取决于你想要实现的目标,例如,not like ('R%') - 其中 R 不是文本的开头。

英文:

Consider rewriting like this:

FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04] 
   
WHERE TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')  AND (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL)

order by BOOKING_DATE_STMT

You might want to look at how you have written your "not like", it should include the wildcards depending on what you want it to achieve

for example, not like ('R%') - where R does not start the Text

huangapple
  • 本文由 发表于 2023年7月27日 18:55:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定