War es hilfreich?

Lösung

Old style SQL für eine volle verbinden - A mit B verkettet, ohne Zeilen in B auch in A (in der Mitte):

-- all rows in A with or without matching B
select a.x, a.y, a.z 
  from a
       left join b
         on a.x = b.x
        and a.y = b.y
union all
-- all rows in B with no match in A to "exclude the middle"
select b.x, b.y, null as z
  from b
 where not exists (select null
                     from a
                    where b.x = a.x
                      and b.y = a.y)

ANSI Stil:

select coalesce(a.x, b.x) as x,
       coalesce(a.y, b.y) as y,
       a.z
  from a 
       full outer join b
         on a.x = b.x
        and a.y = b.y

Die coalesce ist gibt es für die Sicherheit; Ich habe eigentlich nie Ursache hatte eine vollständige äußere Verknüpfung in der realen Welt zu schreiben.

Wenn das, was Sie wirklich wollen, um herauszufinden, ob zwei Tabelle identisch sind, gehen Sie wie folgt:

SELECT COUNT(*) 
  FROM (SELECT list_of_columns
          FROM one_of_the_tables
         MINUS
        SELECT list_of_columns
          FROM the_other_table
        UNION ALL
        SELECT list_of_columns
          FROM the_other_table
         MINUS
        SELECT list_of_columns
          FROM one_of_the_tables)

Wenn das liefert ein Nicht-Null-Ergebnis, dann gibt es einen Unterschied. Es ist Ihnen nicht sagen, welche Tabelle es ist in, aber es ist ein Anfang.

Andere Tipps

Sounds wie das ist im Grunde, was Sie wollen. Wer passt Zeilen zwischen zwei Tabellen auf Spalten Y und Z, finden die nicht ausgeglichenen Reihen und geben die Werte der Spalten X, Y und Z.

SELECT a.x, a.y, a.z, b.x, b.y, b.z
  FROM a FULL OUTER JOIN b ON a.y = b.y AND a.z = b.z
  WHERE a.y IS NULL OR b.y IS NULL
scroll top