如何在另一个表中查找不存在或不同的行

时间:2018-02-07 06:50:23

标签: sql oracle select

我有两张桌子。 一个是inventorytable,列PO No,index,item name,amount,date

PO  Index   Item    amount  date       unit price
PO01    1   pen     20      1/2/2018    10
PO01    2   pad     15      1/3/2018    10
PO02    1   book    30      2/5/2018    10

另一个表格为paymentrecord PO No,index,item name, amount, value

PO  Index   Item    amount  
PO01    1   pen      10 
PO01    2   pad      15 

如何查找未付款或未全额付款的广告资源?

PO  Index   Item    amount   unpaid value 
PO01    1   pen     10        100
PO02    1   book    30        3000

我试过跟随sql

select * 
from inventory 
where not exists (select inventory.PONo,inventory.index 
                  from inventory,payment
                  where inventory.PO=payment.PO )

但它什么都不返回

4 个答案:

答案 0 :(得分:1)

使用外部联接将付款链接到广告资源。过滤export default muiThemeable()(ComponentName); 小于库存项目的计算值payment.amount

unit_price * amount

注意:所有示例中的内容名称都不一致。因此,您可能需要调整此解决方案以使其在您的架构上运行。

答案 1 :(得分:0)

我猜你需要PO和Index两者来唯一地识别记录。 试试下面。

select * from inventory 
where not exists 
(select 1 from inventory,payment where inventory.PO=payment.PO and inventory.Index=payment.Index)

答案 2 :(得分:0)

您可以使用以下查询

select i.ipo, i.iindex, i.iitem, 
(i.iamount - coalesce(p.pamount,0)) as Tamount
from payment p right outer join invent i
on (i.ipo = p.ppo and i.iindex = p.pindex and i.iitem = p.pitem)
where i.iamount != coalesce(p.pamount,0)

这是

的结果
ipo iindex  iitem   Tamount
PO01    1   pen 10
PO02    1   book    30

后面的表结构是

create table invent
(
iPO  varchar(4),
iIndex   int, 
iItem   varchar(10),
iamount  int,
idate       date,
iunit_price int);

create table payment
(
pPO  varchar(4),
pIndex   int,
pItem    varchar(10),
pamount  int);

答案 3 :(得分:-1)

希望以下查询有帮助。使用相同的列名而不是*。

select * from inventory
Except
select * from payment
相关问题