table 1 and table 2 have their primary key (key1,key2) to be the same. table 3 is a user defined table with one column field5 which is common to table2. I need to select the rows from table 1 and table 2 where field5 in table2 is in table3. I need to obtain the rows in the same order as table3.
table 1
key1 key2 field1 field2
table 2
key1 key2 field3 field4 field5
user defined table
field5
the query i am thinking is
select a.key1, a.key2, a.field1, a.field2, b.field3, b.field4
from table1 as a INNER JOIN table2 as b ON a.key1 = b.key1 AND a.key2 = b.key2
where b.field5 in (select field5 from table3)
this will not give me the rows in the same order as in table3.
I would need something like this to maintain the order?
select a.key1, a.key2, a.field1, a.field2, d.field3, d.field4 from
table1 as a INNER JOIN
(select b.key1, b.key2, b.field3, b.field4
from table2 as b INNER JOIN table3 as c
ON b.field5 = c.field5) as d
WHERE a.key1 = d.key1 AND a.key2 = d.key2