views:

279

answers:

2

I need to merge two query results as in union, but I want to only keep the difference between the two results... Is this possible?

I'm basically selecting ALL resources in Query 1, and NOT-ALLOWED resources in Query 2, I obviously need the ALLOWED resources in my last result...

In pseodo-code:

Query1 - Query2

Queryresult 1:

+-------+
|  id   |
+-------+
|   1   |
+-------+
|   2   |
+-------+
|   3   |
+-------+
|   4   |
+-------+
|   5   |
+-------+
|   6   |
+-------+

Queryresult 1:

+-------+
|  id   |
+-------+
|   2   |
+-------+
|   5   |
+-------+

Needed:

+-------+
|  id   |
+-------+
|   1   |
+-------+
|   3   |
+-------+
|   4   |
+-------+
|   6   |
+-------+
+4  A: 

Like this, using NOT IN:

SELECT id FROM queryOneTable
WHERE id NOT IN (
    SELECT id FROM queryTwoTable
)
nickf
so easy, yet so working :)
Ropstah
A: 

I tested this query in SQLExpress, since I don't have MySql. I'm assuming it works the same way.

select id
from x 
left join y on x.id = y.id
where y.id is null
John Fisher