简体   繁体   中英

list all items from one table that are not in another

I have two tables in a database that I would like to compare. One is a list of inventory called stock and the other is a list of items ordered called items. I would like to list all inventory from the stock table that has never been ordered (so I basically want to check for any items that are in stock but NOT in items). I'm having some confusion because stock contains two keys, namely stock_num AND manu_code (code for manufacturer). Below is what I have so far, which I'm hoping is close or at least headed in the right direction. It presently returns an empty list with no errors.

select stock.stock_num, stock.manu_code, stock.description
from stock
        join items 
                on stock.stock_num = items.stock_num
                and stock.manu_code = items.manu_code
where items.stock_num and items.manu_code is null

Use LEFT OUTER JOIN :

select stock.stock_num, stock.manu_code, stock.description
from stock
left outer join items 
  on stock.stock_num = items.stock_num
  and stock.manu_code = items.manu_code
where items.stock_num is null and items.manu_code is null

Try this

select stock_num ,manu_code from  stock
except 
select stock_num,manu_code from items

you can get Which is not in Items Table

Refer : Except

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM