2017-09-07 5 views
1

Ich habe drei Tabellen Tabelle 1, Tabelle 2, table3innere Verknüpfung table2 OP-Tisches 3

Tabelle1 und Tabelle2 sind Haupttabellen mit fast gleichem Aufbau

table3 mit beiden Tabellen verknüpft ist, hängt die Hälfte bespielen tabelle1 und die Hälfte auf table2

ich möchte so etwas wie dieses

select a.column1,a.column2, c.column1 
from table3 c 
    inner join table1 a ON a.pkey = c.fkey 
    OR inner join table2 a ON a.pkey = c.fkey 
+1

ODER? Ist es nicht wichtig, an welcher Tabelle Sie teilnehmen? – jarlh

+0

Gibt es in der Tabelle3 eine Diskriminantenspalte, die den Unterschied anzeigt? – Serg

+0

Es gibt keine Diskriminanten Spalte in Tabelle 3 –

Antwort

1

Sie union all verwenden könnte, dieses Verhalten zu emulieren:

SELECT a.column1, a.column2, c.column1 
FROM table3 c 
INNER JOIN (SELECT column1, fkey 
      FROM table1 
      UNION ALL 
      SELECT column1, fkey 
      FROM table2) a ON a.pkey = c.fkey 
+0

7 Sekunden schneller ... – jarlh

+0

Tabelle 1 und Tabelle 2 hat dann lakhs Datensatz (SELECT Spalte1, fKey FROM Tabelle1 UNION ALL SELECT spalte1, fkey FROM table2) würde die Leistung nicht beeinträchtigen? –

+1

UNION ALL wird nicht deduplizieren und daher wäre es schneller – SriniV

1

Ich würde vorschlagen, left join verwenden, wenn Sie über die Leistung kümmern:

select coalesce(t1.column1, t2.column1) as column1, 
     coalesce(t1.column2, t2.column2) as column2, 
     c.column1 
from table3 c left join 
    table1 t1 
    on t1.pkey = c.fkey left join 
    table2 t2 
    on t2.pkey = c.fkey 
where t1.pkey is not null or t2.pkey is not null; 

Dieser Vorteil des Indizes auf table1(pkey, column1, column2) und table2(pkey, column1, column2) nehmen.

+0

Danke, das hat für mich funktioniert –

Verwandte Themen