如何使用SQL中的where exists?
这就是我的问题——写一个select语句,从gla表返回两列:acc.num和acc.descrp。结果应具有从未使用过与inv.line表的完全外部联接的帐号。从未使用过的部分让我感到困惑,我的代码给了我缺少的关键字错误。有什么想法吗如何使用SQL中的where exists?,sql,oracle,Sql,Oracle,这就是我的问题——写一个select语句,从gla表返回两列:acc.num和acc.descrp。结果应具有从未使用过与inv.line表的完全外部联接的帐号。从未使用过的部分让我感到困惑,我的代码给了我缺少的关键字错误。有什么想法吗 select Account_Number, Account_Description from GENERAL_LEDGER_ACCOUNTS gla full join INVOICE_LINE_ITEMS inv where not exists (sel
select Account_Number, Account_Description
from GENERAL_LEDGER_ACCOUNTS gla full join INVOICE_LINE_ITEMS inv
where not exists
(select *
from
where inv.ACCOUNT_NUMBER= Gla.ACCOUNT_NUMBER
)
order by Account_number
我是MSSQL开发人员,但基于此示例 你会变成
select Account_Number, Account_Description
from GENERAL_LEDGER_ACCOUNTS gla
-- when you use join, you need to specify the relation between which column and which column this join
full join INVOICE_LINE_ITEMS inv on gla.ACCOUNT_NUMBER= inv.ACCOUNT_NUMBER
where not exists
(select 1
from
where inv.ACCOUNT_NUMBER= Gla.ACCOUNT_NUMBER
)
但是从你的问题来看,你想要所有没有在任何发票中使用的账户,你可以通过简单的方式做到这一点
select Account_Number, Account_Description
from GENERAL_LEDGER_ACCOUNTS gla
where not exists
(select 1
from INVOICE_LINE_ITEMS inv
where inv.ACCOUNT_NUMBER= Gla.ACCOUNT_NUMBER
)
希望这将帮助您您可以使用not inMSSQL子句来获取此信息
select Account_Number, Account_Description
from GENERAL_LEDGER_ACCOUNTS
where ACCOUNT_NUMBER not in(select ACCOUNT_NUMBER from INVOICE_LINE_ITEMS)
这相当于不存在查询。我不确定您是否清楚地了解您的需求,您为什么不使用该查询:
select gla.Account_Number, gla.Account_Description
-- ^^^^ ^^^^
-- specify the table alias in case of duplicate columns
from GENERAL_LEDGER_ACCOUNTS gla full join INVOICE_LINE_ITEMS inv
-- ^^^^^^^^^
-- ??? see note below
on inv.ACCOUNT_NUMBER = Gla.ACCOUNT_NUMBER
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- join clause
where inv.ACCOUNT_NUMBER IS NULL
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^
-- as this is a full outer join, some rows in `gla`
-- might not have a matching row in `inv`
-- This will keep only those rows
order by Account_number
另外,根据你的描述,我想说你只需要一个左连接,而不是一个完整的外部连接 我想你需要在gla中的哪一列和inv中的哪一列之间加入to join on关键字。你如何向我展示如何使用上面的代码,请感谢@HadiHassanWhat's outer join gla to inv on?您需要使用ON子句来表示这一点,正如@Hadi已经指出的那样