如何使用SQL中存在的位置?

时间:2015-01-17 06:20:21

标签: sql oracle

所以这是我的问题 - 编写一个select语句,返回gla表中的两列:acc.numacc.descrp。结果应该具有使用inv.line表的完全外连接从未使用过的帐号。从未使用的部分让我感到困惑,我的代码让我错过了关键字错误。任何想法?

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

3 个答案:

答案 0 :(得分:1)

我不确定是否清楚地了解您的需求,为什么您没有使用它呢?

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

此外,根据您的描述,我会说您只需LEFT JOIN - 而不是FULL OUTER JOIN

答案 1 :(得分:0)

我是MSSQL开发人员,但基于此w3schools示例

你的查询将成为

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
 )

希望这会对你有所帮助

答案 2 :(得分:0)

您可以使用not in(MSSQL)子句来实现此目的。

select Account_Number, Account_Description
from GENERAL_LEDGER_ACCOUNTS 
where ACCOUNT_NUMBER not in(select ACCOUNT_NUMBER from INVOICE_LINE_ITEMS)

这相当于不存在查询。