mardi 14 juin 2016

get overlapping records if 2 tables have an overlapping column

I have a complex query which can return 10000+ records.

I want to be able to filter the query based on 2 tables. I want to select the record only if ANY record in table 1 also exists in table 2.

For instance if my table 1 and 2 are this.

table 1.

ID
1
2
3
5
6
7

table 2.

ID
100
200
1
300
600

I want to select the record. However if table 1 and table 2 are this. I dont't want to select the record. since table 1 and table 2 have no overlap.

table 1.

ID
1
2
3
5
6
7

table 2.

ID
100
200
500
300
600

my select statement would look something like this:

SELECT * FROM SOMETABLE AS S
WHERE OVERLAP (SELECT ID IN TABLE1 WHERE S.PID = PID)
              (SELECT ID IN TABLE2 WHERE S.PID = PID)

Aucun commentaire:

Enregistrer un commentaire