MySQL: Add to table3 if record from table1 has a count of 0 or 1 in table 2 -
MySQL: Add to table3 if record from table1 has a count of 0 or 1 in table 2 -
basically i'm trying build table (we'll phone call table3) has records table1 have counts of 0, 1, or not appear @ in table2. table1 , table3 in database1 , table2 in database2.
here closest can getting working.
insert database1.table3 (col1, col2, ...) select t1.col1, t1.col2, ... database1.table1 t1 left bring together database2.table2 t2 on t1.columntomatch = t2.columntomatch (t2.count < 2);
tldr: want table1 doesn't appear in table2 added table3. if record in table1 appears in table2 want include in table3 if table2.count 0 or 1.
thank you
welcome so.
you looking left excluding join
- see here
to records table1
doesn't appear in table2
a
select table1.* table1 left bring together table2 on table1.columntomatch = table2.columntomatch table2.columntomatch null
after matchting records count 0 or 1:
select table1.* database1.table1 t1 inner bring together database2.table2 on table1.columntomatch = table2.columntomatch table2.count in (0, 1)
then can append both results union all
, store whole bunch in new table:
create table table3 select ... ... union select ... ...
mysql
Comments
Post a Comment