jrinkens7
jrinkens7

Reputation: 13

Combine two tables with same columns but different values and return only the not machting ones

I have tables with the same columns but the values are not all the same. I want to see the values from the columns that dont exists in table 1 but exists in table 2.

it works with:

select *
from table_1
where not exists (select * from table_2 where table_1.xxx = table_2.xxx) 

and I join the rest of the columns on each other.

But isn't there a faster way then join every column in the not exists?

Upvotes: 1

Views: 348

Answers (2)

pcdev
pcdev

Reputation: 3062

Your question says "I want to see the values from the columns that dont exists in table 1 but exists in table 2", however your code example is getting you all rows in table_1 where the same row doesn't exist in table_2. So, not knowing which one you want, I present you with this solution, which gives you what you asked for first. If you want it the other way around, swap the table names:

select * from table_2
minus
select * from table_1

Caveats:

  1. Tables must have exactly the same schema.
  2. If you're not sure about the schema, or the tables may change over time you could specify columns in the select statements, but only the specified columns will be matched.

Upvotes: 1

jrinkens7
jrinkens7

Reputation: 13

Thanks! EXPECT doesnt work for me (using Oracle) but instead i should use MINUS. That worked! TY!

Upvotes: 0

Related Questions