Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have Two tables,

- Table A
- Table B

Both the tables have Different Column..How can i check one table Data not exists in another table with Multi Column Condition.here is my Code..but it cant fetch unmatch data..

SQL
Select Code,Name,Size from TableA  where Code Not in (Select Code from TableB) and Name not in (Select Name from TableB) and 
SIZE not in (Select Size from TableB)


Am also using 'Not Exists'..but no Use..

SQL
Select Code,Name,Size from TableA Tb1  where not exists (Select Code from TableB Tb2 where Tb1.Code != Tb2.CODE and Tb1.Name != Tb2.Name and Tb1.Size != Tb2.SIZE) 


Suggest me any other Function..Thank You...
Posted
Updated 16-Nov-13 23:01pm
v3

You can use an outer join like this:
SQL
Select  Code,Name,SIZE
FROM    TableA a
left OUTER JOIN TABLEB b
    ON  a.code = b.code
    AND a.Name = b.Name
    AND a.SIZE = b.SIZE
WHERE   b.code IS NULL
OR      b.name IS NULL
OR      b.SIZE IS NULL
If you had used Oracle you could have used an IN clause like this:
SQL
Select  Code,Name,SIZE
FROM    TableA a
WHERE   (Code,Name,SIZE) NOT IN (SELECT Code,Name,SIZE FROM TableB)


<edit>I revisited because of Maciejs comment and realized what was wrong with your "Exists" query, it should look like this:
SQL
Select  Code,Name,SIZE
FROM    TableA Tb1
WHERE NOT EXISTS (
    SELECT  1
    FROM    TableB Tb2
    WHERE   Tb1.Code = Tb2.CODE
    AND     Tb1.Name = Tb2.Name
    AND     Tb1.Size = Tb2.SIZE
    ) 
In the end it's probably getting the same plan as the join query but it's worth testing.</edit>
 
Share this answer
 
v4
Comments
Maciej Los 17-Nov-13 7:22am    
Looks well! +5!
Please, see my answer ;)
Solution1 is good and i would suggest you to read this article: Visual Representation of SQL Joins[^]. It will help you to understand how joins work.
 
Share this answer
 
Comments
Jörgen Andersson 17-Nov-13 14:36pm    
Yes, it's a brilliant article in its simpleness.
Tom Marvolo Riddle 20-Nov-13 6:29am    
5!.Thanks for suggesting this article
Maciej Los 20-Nov-13 14:02pm    
You're welcome ;)
SQL
Select Code,Name , Size from TableA a
where CODE NOT IN (Select CODE FROM TableB WHERE SIZE=a.SIZE) AND
SIZE NOT IN (Select SIZE FROM TableB WHERE CODE=a.CODE)



Try this
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900