Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

sql - finding missing data between two tables mysql workench

I have some difficulties to answer the following questions

1 Write an SQL statement to show the SKU and SKU_Description for all items that have not been ordered.

SELECT SKU,SKU_DESCRIPTION 
FROM SKU_DATA
INNER JOIN ORDER_ITEM
ON SKU_DATA.SKU =ORDER_ITEM.SKU;

I do not know how to write the condition to compare and display the SKU that is present in SKU_Data but are not present on order_item...

Here the different tables available

Inventory (WarehouseID,SKU,SKU_Description,QuantityOnHand,QuantityOnOrder)

Order_Item(OrderNumber,SKU,Quantity,Price,ExtendedPrice)

Retail_order(OrderNumber,StoreNumber,StoreZip,OrderMonth,OrderYear,OrderTotal)

SKU_Data(SKU,SKU_Description,Department,Buyer)

Warehouse(WarehouseCity,WarehouseState,Manager,SquareFeet)

Thank you

question from:https://stackoverflow.com/questions/66057207/finding-missing-data-between-two-tables-mysql-workench

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not much sure. But from your query, It seems that you need not exists as follows:

SELECT SKU,SKU_DESCRIPTION 
FROM SKU_DATA
WHERE NOT EXISTS
      (SELECT 1 FROM ORDER_ITEM OI
         JOIN RETAIL_ORDER RO ON OI.ORDERNUMBER = RO.ORDERNUMBER
        WHERE SKU_DATA.SKU =OI.SKU
          AND RO.ORDERYEAR >= 2012);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...