![](/img/trans.png)
[英]SQL query: list all items in one table that do not appear in another table
[英]list all items from one table that are not in another
我要比較的數據庫中有兩個表。 一個是稱為庫存的庫存清單,另一個是稱為物料的已訂購物料清單。 我想從庫存表中列出從未訂購的所有庫存(因此我基本上想檢查是否有庫存但沒有庫存的物品)。 我有些困惑,因為庫存包含兩個鍵,即stock_num和manu_code(制造商代碼)。 以下是我目前為止所擁有的,我希望它們接近或至少朝着正確的方向前進。 它當前返回一個沒有錯誤的空列表。
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
使用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
嘗試這個
select stock_num ,manu_code from stock
except
select stock_num,manu_code from items
您可以獲得項目表中未包含的內容
參考: 除外
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.