简体   繁体   English

列出一个表中不在另一个表中的所有项目

[英]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

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 参考: 除外

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL查询:列出一个表中未出现在另一个表中的所有项 - SQL query: list all items in one table that do not appear in another table SQL 查询 - 一个表中而不是另一个表中的项目列表 - SQL query - list of items in one table not in another SQL怎么做? 从一个表中选择所有项目,然后根据另一张表填充空白? - SQL How To? Select all items from one table, and fill in the gaps based on another table? 将一个表中的项目插入另一个表 - Inserting items from one table into another 选择一个表中的所有项目并加入另一个表,允许空值 - Selecting all items in one table and join with another table, allowing nulls MS ACCESS 查询联结表,用于一个表中的所有项目,但不在另一个表中 - MS ACCESS Query with junction table, for all items in one table, but not in another 从一个表中获取项目列表,并从另一个表中添加汇总评分 - Getting a list of items from one table and adding aggregated ratings from another 如何基于另一个表中的HABTM关系从一个表中选择项目? Postgres“ ALL”不起作用, - How to select items from one table based on HABTM relation in another? Postgres “ALL” does not work, Postgres,从一个表中获取所有项目,其中 id 不在另一个表 JSON 数组列中? - Postgres, get all items from one table which ids are not in another tables JSON array column? 如何创建表列,该列是另一个表中的项目的列表 - how to create table column that is a list of items from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM