I have two tables that have the same columns. If a change occurs it is being recorded in table2 and then I compare table1 to table2 and see if they are different. If they are different I conclude there was a change and I would like to display that in my resulting table.
For example:
SELECT t1.name, t1.age, t1.profession, column_changed, old_value, new_value
FROM table1 t1, table2 t2
WHERE t1.column1<>t2.column1
OR t1.column2<>t2.column2
OR t1.column3<>t2.column3
Of course this query isn’t correct. I would like the column_changed, old_value, new_value display the relevant values.
Any ideas?
After further brain storming this I have concluded that ganders solution works better with slight improvment. The improvement is a while loop and a count variable. We need to have that in case there are several columns changed at once and not just one. It will, however, result in outputting NULL’s as well so you can just delete them. Here is the modified query: