0
votes

Trying to merge two game servers' tables that have this structure:

map | authid | name | time | date | ...

I would like to replace a row only if the time value of table2 is less than that of table1 AND ONLY if the map and authid values are BOTH the same. If the time values in table2 are greater, then the row from the current table (table1) should be kept untouched. Otherwise (on different map or authid values), the row from table2 should simply be appended to table1.

1
please provide example data and table schema information for us to help you. - Praveen Lobo

1 Answers

0
votes

My way would be to 1st: create a view for replacing rows and another with the correct result set that should be appended like

--view for update
create view ChangeTable1 as select table2.map, table2.authid,table2.name, table2.date, table1.map as t1map, table1.authid as t1authid...from table1 to inner join table2 on table1.map=table2.map and table1.athid=table2.autid where table1.time>table2.time

-- view for appending
create view Add2Table1 select table2.map, table2.authid,table2.name, table2.date... from table2 where concat(table2.map, table2.authid) not in (select concat(table1.map, table1.authid) from table1)

-- update statement based on first view ChangeTable1
update ChangeTable1 set t1date=date, t1somevalue=somevalue......

-- insert Statement based an second view Add2Table1

insert into table 1 (map, authid, name, time, date, .... as select map, authid, name, time, date,... from Add2Table1

I hope this helps. I mostly do MS SQL, so there might be some syntax issues that need translation to MYSQL, Nils
If you need a permanent process doing this, you might consider putting this in a stored procedure