[英]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).
我有些困惑,因为库存包含两个键,即stock_num和manu_code(制造商代码)。 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
: 使用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.