0

I have three tables Tab1, Tab2 and Tab3 with almost same structre (in MS Access). But Tab2 and Tab3 have a few more columns than Tab1.

Tab2 and Tab3 are exactly same structure. Following are the joining keys

col1
col2
col3

Basically Tab1 records should tally with Tab2 and Tab3 together. If I need to get the missing records in Tab2 and Tab3 when compare to Tab1 what could be the efficient way

Appreciate your response

4

2 に答える 2

0

通常は、一緒に LEFT JOIN された tab2 と tab3 を SELECT FROM tab1 LEFT JOIN します。そうすれば、tab1 からすべてのレコードを取得できます。tab2 と tab3 に欠落しているレコードがある場合、null になります。WHERE句でnullをチェックできます

したがって、クエリは次のようになります (角かっこに注意してください。これは ms-access の要件です)。

SELECT * FROM
tab1 LEFT JOIN (tab2 LEFT JOIN tab3 ON tab2.col1 = tab3.col1 AND tab2.col2 = tab3.col2)
ON tab1.col1 = tab2.col1 AND tab1.col2 = tab2.col2 
WHERE tab2.col1 Is Null;
于 2013-02-28T00:14:49.783 に答える
0

キーのみを気にする場合は、次のアプローチが適しています。

select col1, sum(isTab1) as numTab1, sum(isTab2) as numTab2, sum(isTab3) as numTab3
from ((select col1 as col, 1 as isTab1, 0 as isTab2, 0 as isTab3 from tab1
      ) union all
      (select col2, 0 as isTab1, 1 as isTab2, 0 as isTab3 from tab2
      ) union all
      (select col3, 0 as isTab1, 0 as isTab2, 1 as isTab3 from tab3
      )
     ) t
group by col
having sum(isTab1)*sum(isTab2)*sum(isTab3) <> 1

これにより、各キー値が返され、3 つのテーブルすべてに含まれていないキーについて、それらが含まれているテーブルと含まれていないテーブルがわかります。おまけとして、これにより、テーブルのいずれかに重複キーがあるかどうかもわかります。

于 2013-02-28T00:11:41.257 に答える