Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using intersect on more the two tables?

I am trying to find the rows that all four tables share in common. I tried using this:

SELECT * 
FROM TABLE1 
INTERSECT
SELECT *
FROM TABLE2
INTERSECT
SELECT *
FROM TABLE3
 INTERSECT
SELECT *
FROM TABLE4;    

But got no results, i know there are rows that meet this requirement. Because using union instead and adding "order by" i see four rows with the same description from different tables.

like image 902
TheMineral Avatar asked Jul 04 '13 07:07

TheMineral


1 Answers

Use INNER JOIN with conditions on the fields that you want
ex:

SELECT 
    t1.* 
FROM 
    TABLE1 t1
INNER JOIN TABLE2 t2
    ON t1.field1 = t2.field1
    AND t1.field2 = t2.field2
    ...
INNER JOIN TABLE2 t3
    ON t1.field1 = t3.field1
    AND t1.field2 = t3.field2
...
like image 76
Stephan Avatar answered Sep 27 '22 22:09

Stephan