请教oracle 查询语句写法

ynquan 2021-02-09 01:48:27
select * from t where (t.code='A' or t.code='B') and 'A'='A' or t.code<>'A' and t.code<>'B' and 'A'='B'
oracle 中我看不懂 'A'='A' 和 'A'='B' 这种写法。是什么意识?
我运行后得到的结果是跟select * from t where (t.code='A' or t.code='B')一样的。
那为什么这么写 (t.code='A' or t.code='B') and 'A'='A' or t.code<>'A' and t.code<>'B' and 'A'='B'。
我以前用的是mssql数据库的,现在用oracle后看到这是语句不知道为什么这么写。
请高手解答。十分感谢。
...全文
197 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nayi_224 2021-02-17
  • 打赏
  • 举报
回复
根据我的经验你应该少写了两个括号
select * from t 
 where (
       (t.code='A' or t.code='B') 
   and 'A'='A' )
   or (t.code<>'A' 
   and t.code<>'B' 
   and 'A'='B')
至少这种写法在java程序员中还算常见。逻辑与或的活用经常用来替代持久层中不美观的标签,但是你上面这种处理更像是在用sql处理一些应用逻辑。举例来说,你上面语句的源码很可能是这样的
select * from t 
 where (
       (t.code='A' or t.code='B') 
   and 'A'=#{state} )
   or (t.code<>'A' 
   and t.code<>'B' 
   and 'B'=#{state})
state是前台传来的状态码。当状态为A时,筛选code为AB的,为B时,筛选code不为AB的
微风轻轻 起 2021-02-09
  • 打赏
  • 举报
回复
这种写法很少,a=a 就是条件为真,a=b就是条件为假。我之前写Oracle,也没这样写过。总之不常见,不用太过纠结

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧