views:

51

answers:

4

I want to intersect two "select" queries and then put another condition for the whole result. I mean:

(select ... intersect select ...) where ...

is it possible?

+2  A: 

Select * From (select ... intersect select ...) as intersectedTable where ...

Matin Habibi
+3  A: 

Depends on your flavour of database Oracle (for example) will allow this:

SELECT *
  FROM ( SELECT *
           FROM TABLE_A
         INTERSECT
         SELECT *
           FROM TABLE_B
       )
WHERE <conditions>
Mark Baker
A: 

Since you are intersecting, any condition you put on the either of the two SELECTs will also apply to the whole condition...

So, the easiest way is to just

SELECT ... FROM ... WHERE (condition)
INTERSECT
SELECT ... FROM ... WHERE (another condition)        
BlueRaja - Danny Pflughoeft
+2  A: 

You want to use a derived table.

    SELECT value,data FROM
    (SELECT value,data FROM table1 union select value,data from table2) t
    WHERE value=5;
Gary