考えられる3つのケースを処理するソリューションを次に示します。
- t2 にない t1 に ch_code 値があります
- t1 にない t2 に ch_code 値があります
- t1 と t2 の両方に ch_code 値があります
SELECT t1.ch_code, t1.ch_def, t1.ch_weight, '' as address from t1 where not exists (select * from t2 where t2.ch_code = t1.ch_code)
UNION
SELECT t2.ch_code, '' as ch_def, '' as ch_weight, t2.address from t2 where not exists (select * from t1 where t1.ch_code = t2.ch_code)
UNION
SELECT t1.ch_code, t1.ch_def, t1.ch_weight, t2.ch.address from t1 left join t2 on t1.ch_code = t2.ch_code
その結果セットを取得したら、マージされたデータを格納するための新しいテーブルがある場合は、INSERT INTO を実行できます。