select - MySQL - Combine two tables into third without duplicates -
i having hard time wrapping mind around concept here. have 3 tables identical structure. need table 1
combined table 2
, insert into
table 3
. issue:
table 1
, table 2
have duplicate content, exception being id
. not using id
other ai
however, not concern.
an example of looking accomplish is:
original table 1 _________________________________________________________ | id | col 2 | col 3 | col 4 | |---------------------------------------------------------| | 1 | stuff_1 | stuff_2 | stuff_3 | |---------------------------------------------------------| | 2 | stuff_x | stuff_y | stuff_z | |_________________________________________________________|
combine with
original table 2 _________________________________________________________ | id | col 2 | col 3 | col 4 | |---------------------------------------------------------| | 3 | stuff_1 | stuff_2 | stuff_3 | |---------------------------------------------------------| | 4 | stuff_a | stuff_b | stuff_c | |_________________________________________________________|
to make
combined table 3 (notice id's not counted in duplicate check) _________________________________________________________ | id | col 2 | col 3 | col 4 | |---------------------------------------------------------| | ? | stuff_1 | stuff_2 | stuff_3 | |---------------------------------------------------------| | ? | stuff_x | stuff_y | stuff_z | |---------------------------------------------------------| | ? | stuff_a | stuff_b | stuff_c | |_________________________________________________________|
i have tried insert table 3 select * table 2;
, doing insert ignore
table 1. i've tried replace into
well, because omitting id, don't work because column count off. have ideas how combine these 2 third table while dealing id issue? reiterate not matter id's not ever used.
you can use group by
flatten out duplicates while leaving id
value.
this give unique col1
, col2
, col3
values, , set id
minimum id
value rows merged duplicated:
insert table3 (id, col1, col2, col3) select min(id), col1, col2, col3 ( select id, col1, col2, col3 table1 union select id, col1, col2, col3 table2 ) table1and2 group col1, col2, col3
with posted data, result should this:
id col1 col2 col3 -- ------- ------- ------- 1 stuff_1 stuff_2 stuff_3 2 stuff_a stuff_b stuff_c 4 stuff_x stuff_y stuff_z
Comments
Post a Comment