Apologies if the pseudo idea is a little confusing.
Basically I have 3 tables, the tables do not have an exact match on an attribute.
I need to compare whether any of values 2-5 from table 1 either match any value from table 2 on a certain attribute
OR
whether any of values 2-5 from table 1 either match table 3 on a certain attribute
If there is match, they will then be inserted into the newtable.
When I run either Inner join without the other, I seem to receive the filtered results I wanted from either statement. When I add them together, I don't get the correct results.
Is there any way to combine to inner joins or use any other sort of syntax/command to make this comparative search/insert?
Thanks in advance for any help you can offer!
INSERT
INTO newtable
(
attribute1new
attribute2new
attribute3new
attribute4new
attribute5new
attribute6new
)
SELECT
t1.attribute1,
t1.attribute2,
t1.attribute3,
t1.attribute4,
t1.attribute5,
t1.attribute6
FROM table1 t1
INNER JOIN table2 t2
ON t1.attribute2 = table2.attribute9
OR t1.attribute3 = table2.attribute9
OR t1.attribute4 = table2.attribute9
OR t1.attribute5 = table2.attribute9
INNER JOIN table3 t3
ON t1.attribute2 = table3.attribute8
OR t1.attribute3 = table3.attribute8
OR t1.attribute4 = table3.attribute8
OR t1.attribute5 = table3.attribute8
;
You can use EXISTS
to get the expected result:
SELECT
t1.attribute1,
t1.attribute2,
t1.attribute3,
t1.attribute4,
t1.attribute5,
t1.attribute6
FROM table1 t1
where exists (select * from table2 t2
where t1.attribute2 = t2.attribute9
OR t1.attribute3 = t2.attribute9
OR t1.attribute4 = t2.attribute9
OR t1.attribute5 = t2.attribute9)
OR
exists (select * from table3 t3
WHERE t1.attribute2 = t3.attribute8
OR t1.attribute3 = t3.attribute8
OR t1.attribute4 = t3.attribute8
OR t1.attribute5 = t3.attribute8)