首页 > 解决方案 > 如何编写“选择查询”以仅使用一张表查找一个帐户的借方和另一个帐户的贷方之间的差异?

问题描述

sql语句:

SELECT debit,credit FROM transactionentries where glaccountid = 15374;
SELECT debit,credit FROM transactionentries where glaccountid = 15376;

提示:debit此 glaccountid 的15374必须等于credit其他 glaccountid 的 ,15376反之亦然。

我想要得到的是返回一个账户的借方不等于另一个账户的贷方的交易条目。

有什么帮助吗?

提前致谢。

我的尝试:

SELECT debit,credit FROM transactionentries 
where glaccountid = 15374 
and debit not in (
  SELECT credit FROM transactionentries where glaccountid = 15376
);

上面的查询不返回任何行,我希望返回一个帐户的借方不等于另一个帐户的贷方的交易条目。

标签: oracleoracle11g

解决方案


select * from transactionentries s where not exists (select 1 from transactionentries p where p.debit=s.credit and s.debit=p.credit);


推荐阅读