我需要历史表的 PHP-MYSQL 查询(代码):我知道我必须做什么以及如何做 - 但不知道如何为它编写 mysql 查询以及如何在 php 页面中列出。以下是我的想法和想要实现的。
我有产品表:product_table
id | cat_id | Subcat |productname | weight | assigned_to | status_id
---------------------------------------------------------------------------
1 | 11 | 2 | P1 | 5.8 | 8 | 1
2 | 12 | 3 | P2 | 6.7 | 27 | 2
它们主要在 product_table 下的更多列,例如 add_date、update_date ..etc
现在,如果我插入或更新我的产品表(product_table),那么它的每条记录无论是新的(插入)还是更新记录,都应该在我的历史表中插入/更新( history_table) 为:
*下面是当新值插入到 product_table 并且同样进入 under history table 时的情况,如下所示:*
id | pid | cat_id | sub_cat | old_value | new_value | filed_name | date_m
---------------------------------------------------------------------------------------------------------
1 | 1 | 11 | 2 | 0 | P1 | productname | 2014-01-17 05:05:21
2 | 1 | 11 | 2 | 0 | 5.8 | weight | 2014-01-17 05:05:21
3 | 1 | 11 | 2 | 0 | 8 | assigned to | 2014-01-17 05:05:21
4 | 1 | 11 | 2 | 0 | 1 | status | 2014-01-17 05:05:21
现在在产品表下:记录 1 更新然后在历史表下同样如此:
id | pid | cat_id | sub_cat | old_value | new_value | field_name | date_m
--------------------------------------------------------------------------------------------------------
1 | 1 | 11 | 2 | P1 | P2 | productname | 2014-01-18 03:05:22
2 | 1 | 11 | 2 | 5.8 | 5.4 | weight | 2014-01-18 03:05:22
3 | 1 | 11 | 2 | 8 | 7 | assigned to | 2014-01-18 03:05:22
4 | 1 | 11 | 2 | 1 | 4 | status | 2014-01-18 03:05:22
因此,当我看到我的 ID 为 1 的产品页面 历史记录时,它将如下所示:
Date | User | field_name | Change
-----------------------------------------------------------------------------------
2014-01-17 05:05:21 | admin | productname | => P1
2014-01-17 05:05:21 | admin | weight | => 5.8
2014-01-17 05:05:21 | admin | assigned to | => 8
2014-01-17 05:05:21 | admin | status | => 1
2014-01-18 03:05:22 | admin | productname | P1 => P2
2014-01-18 03:05:22 | admin | weight | 5.8 => 5.4
2014-01-18 03:05:22 | admin | assigned to | 8 => 7
2014-01-18 03:05:22 | admin | status | 1 => 4
因此,这就是我想实现的,但没有得到在插入和更新时应该编写什么代码(mysql),所以 product_table 中的一些(我想要的)列应该在 history_table 的 2 列中插入/更新并按照上面定义的方式重新设置。以及如何在我的产品页面上显示历史记录。
最佳答案
在表product_table
上创建AFTER INSERT TRIGGER
和AFTER UPDATE TRIGGER
。
检查这个链接
关于mysql - 查询将父表中的 n 列插入或更新到历史表的 2 列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21200593/