mysql - Select columns which are different between 2 table versions -
let's have following table structure:
table1 fixedcolumn1 fixedcolumn2 changingcolumn1 changingcolumn2 test1 test11 1 3 test2 test22 2 5
same table1
updated , saved in table same structure
sametable1updated fixedcolumn1 fixedcolumn2 changingcolumn1 changingcolumn2 test1 test11 77 23 test2 test22 22 5 test3 test33 5 null
now, have following table in want save modified data:
differencestable fixedcolumn1 fixedcolumn2 change columnname newvalue oldvalue test1 test11 modified changingcolumn1 77 1 test1 test11 modified changingcolumn2 23 3 test2 test22 modified changingcolumn1 22 2 test3 test33 added changingcolumn1 5 null
the combination of fixedcolumn1
, fixedcolumn2
unique(like primary key).
i have idea on how in code working datatables
, possible in sql
?
edit:
so table1 updated @ point , saved in sametable1updated (some rows change, removed or added). if row values have changed table1 , still appear in sametable1updated, added parameter change
modified
, oldvalue
tableand new value from
sametable1updated. if row removed table1, appear parameter
changeas
removedand with
oldvalueparameter values from
table1and
newvalue` parameter null records. , other way around newly added rows in table2.
i've explained situation in best way can. please let me know if need more information.
join tables find changingcolumn1 changes. same changingcolumn2 changes. union all
results:
select t1.fixedcolumn1, t1.fixedcolumn2, stu.changingcolumn1 newvalue, t1.changingcolumn1 oldvalue table1 t1 join sametable1updated stu on t1.fixedcolumn1 = stu.fixedcolumn1 , t1.fixedcolumn2 = stu.fixedcolumn2 , t1.changingcolumn1 <> stu.changingcolumn1 union select t1.fixedcolumn1, t1.fixedcolumn2, stu.changingcolumn2 newvalue, t1.changingcolumn2 oldvalue table1 t1 join sametable1updated stu on t1.fixedcolumn1 = stu.fixedcolumn1 , t1.fixedcolumn2 = stu.fixedcolumn2 , t1.changingcolumn2 <> stu.changingcolumn2 order t1.fixedcolumn1, t1.fixedcolumn2
Comments
Post a Comment