0

I have doubt to mount a query to update and / or add data in a table.

I have a table1 that will be supplied with the following rules:

The table1 have the same fields of table2 + status field to show if the register is active ("A") or inactive ("I").

For each record in table1, check if there are new records in table2

If the record exists in table2 and the record doens't exist in table1, includes the information in table 1 - all the fields of that record from table2 + character "A" (active) in the status field.

If the record exists in table2 and the record exists in table1 too, I have to compare id_register from table2 with the id_register from table1

If any data in this row is different, I have to insert a new row in table1 - all fields from the table2 + character "A" (active) in the status field -, AND the line that was different in table1 will be update with a character "I" (inactive) in status field;

If the record was deleted in table2, this row will receive character "I" (inactive) in table1 status field. (Deleted Record = I have to verify the records that exists in table1 and doens't exists in table2)

The records that have no changed will not be updated.

The tables:

table1

id_register name address phone email date value status

table2

id_register name address phone email date value

Thank you!

4

1 回答 1

0

在我看来,您正在尝试记录对表的更改。使用触发器这是非常可行的,但出于几个原因,我建议不要这样做。首先,您最近的记录将始终是您在表 2 中的活动记录,因此使用时间戳可以避免更新。

其次,与其自己构建解决方案,我会推荐一个我过去使用过的模块,即tablelog。这将比尝试自己重新实现它要简单得多。还有更多的眼球和所有这些......

于 2013-04-23T02:24:59.623 回答