我需要历史表的 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 中插入新值时的情况, 并且在历史表下也是如此:*
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 列中插入/更新并重置为列明智如上定义。以及我如何在我的产品页面上显示历史。