Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL, how can I perform a "subtraction" operation?

Suppose I have two tables, which both have user ids. I want to perform an operation that would return all user IDS in table 1 that are not in table 2. I know there has to be some easy way to do this - can anyone offer some assistance?

like image 310
Waffles Avatar asked Dec 09 '22 11:12

Waffles


1 Answers

Its slow, but you can normally accomplish this with something like 'not in'. (There are other functions in various RDBMS systems to do this in better ways, Oracle for instance has a 'exists' clause that can be used for this.

But you could say:

select id from table1 where id not in (select id from table2)
like image 51
dmcnelis Avatar answered Dec 23 '22 05:12

dmcnelis