Waqas Ali
Waqas Ali

Reputation: 93

SQL Query to match if data is not present in another table

I have two tables named tblStockManagement and tblFolding in my database. i have column Name in tblFolding table and column FoldingID as Foreign Key in tblStockManagement table. now i have comboBox in my Winform and i want Names of Items in combobox from tblFolding Table but only those items that are not in tblStockManagement Table.

(because i dont want to select data again if it is already in tblStockManagement table . instead i will update the quantity later).

tblFolding Table

tblStockManagement Table

these are the screenshots of both of tables. please tell me how can i do that

Upvotes: 0

Views: 1525

Answers (3)

jarlh
jarlh

Reputation: 44766

NOT EXISTS version:

select *
from tblFolding f
where not exists (select * from tblStockManagement SM
                  where sm.FoldingID = f.FoldingID)

NOT EXISTS is "NULL safe", which NOT IN isn't.

Upvotes: 1

huMpty duMpty
huMpty duMpty

Reputation: 14460

You can use SQL NOT condition

Select Name
From tblFolding
Where FoldingId Not In (Select FoldingId From tblStockManagement)
Order By Name

Upvotes: 0

Navoneel Talukdar
Navoneel Talukdar

Reputation: 4598

This is you need.Basically a sub query which gets all folding id and using not in operator I exclude those matching sets.

SELECT Name
  FROM tblFolding 
  WHERE FoldingID NOT IN (
    SELECT FoldingID
    FROM tblStockManagement 
  )
;

Upvotes: 0

Related Questions