首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否通过跳过空值来比较列值?

是否通过跳过空值来比较列值?
EN

Stack Overflow用户
提问于 2019-07-29 21:12:02
回答 1查看 69关注 0票数 0

SQL语句一:

代码语言:javascript
复制
select glaccountid,debit,credit 
from transactionentries 
where glaccountid in (15376);

上面的语句返回下面的输出:

代码语言:javascript
复制
+------------------------------+
| glaccountid | debit | credit |
+------------------------------+
|  15376      |  1584 |  null  |
+------------------------------+
|  15376      |  null | 1400   |
+------------------------------+

SQL语句二:

代码语言:javascript
复制
select glaccountid,debit,credit 
from transactionentries 
where glaccountid in (15374);

Above语句返回以下结果:

代码语言:javascript
复制
+------------------------------+
| glaccountid | debit | credit |
+------------------------------+
|  15374      |  null | 1584   |
+------------------------------+
|  15374      | 14000 | null   |
+------------------------------+

我正在尝试编写一个查询,通过忽略具有空值的列,返回15376的借记值与15374的贷记值不相等的事务条目,反之亦然。

我已经尝试过了:

代码语言:javascript
复制
SELECT cpo.glaccountid cpo,cpo.debit,cpo.credit,ba.glaccountid branch, ba.debit,ba.credit 
  FROM transactionentries cpo 
 INNER JOIN transactionentries ba 
    ON cpo.transactionid = ba.transactionid
 WHERE cpo.glaccountid = 15374 
   AND ba.glaccountid = 15376 
   AND (cpo.debit <> ba.credit OR ba.debit <> cpo.credit);

预期输出:

代码语言:javascript
复制
+------------------------------+
| glaccountid | debit | credit |
+------------------------------+
|  15374      |  14000 |  null |
+------------------------------+
|  15376      |  null | 1400   |
+------------------------------+
EN

回答 1

Stack Overflow用户

发布于 2019-07-29 21:31:48

您不能将null当作一个值进行比较,因为null在Oracle中的含义是“无信息”,而不是一个值。关于这一点,你可能会找到很多好的答案。

关于您的查询,如果您想只考虑具有两个not null值的记录,这可以是编辑where条件的一种不言而喻的方法:

代码语言:javascript
复制
(
  (cpo.debit <> ba.credit and cpo.debit is not null and ba.credit is not null)
or
  (ba.debit <> cpo.credit and ba.debit is not null and cpo.credit is not null)
)

你可以用不同的方式编辑它;这是readableI能想到的最多的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57254638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档