There are two tables, namely LongTable and ShortTable. For example, LongTable looks like:
personA | personB
1 | 2
1 | 3
2 | 4
2 | 5
3 | 4
3 | 5
3 | 6
4 | 5
4 | 6
and ShortTable has the following content:
PersonA
1
2
I wonder how to select records from a LongTable (please note that this table is really long, i.e. about 2.000.000 rows) based on records in the ShortTable. So, the result for this particular case should be:
personA | personB
1 | 2
1 | 3
2 | 4
2 | 5
I started with the following query (but with failure: “Can’t reopen table: ‘ShortTable'”):
SELECT * FROM LongTable
WHERE
personA IN (SELECT * FROM ShortTable)
AND
personB IN (SELECT * FROM ShortTable)
Code to reproduce temporary tables is given below.
Thanks in advance for any pointer.
CREATE TABLE LongTable (
personA INT,
personB INT
);
INSERT INTO LongTable VALUES
(1,2),(1,3),(2,4),
(2,5),(3,4),(3,5),
(3,6),(4,5),(4,6);
CREATE TABLE ShortTable (
personA INT
);
INSERT INTO ShortTable VALUES
(1),(2);
Try this