0

I'm using MySQL. I have table with 3 fields: id, price, count. I need to write small billing system and I have faced with some problem. Example :

  1. User need the new computer. User writes price and count to the DB (e.g. 999, 1).
  2. Admin has ability to confirm this price or edit. If admin confirm this price and count i need to save user price and count and admin. Have I need to create duplicate fields(e.g confirmed_price, confirmed_count) and if admin has changed this price have I need to create changed_price and changed_count fields. In this variant table will have many 0 values and would anybody can recommend me another way of solving this problen
4

2 回答 2

0

最简单的方法可能是添加一个额外的列,但这取决于您的情况。

如果您不需要存储原始价格/数量,那么添加一列很容易,并且会像这样工作:

|   ID   |   Price   |   Num   |   Verified   |
+--------+-----------+---------+--------------+
|   1    |    110    |    1    |       1      |
|   2    |    999    |    2    |       0      |

验证时,您可以UPDATE将行和保存时更改Verified为 1。然后,任何具有列的内容都Verified = 0需要审核并Verified = 1进行验证。

因此,假设 ID 2 的 Num 应为 3。您将进行 Admin 编辑:

UPDATE myTable SET Num=3 WHERE ID=2

然后当他们点击“保存”

UPDATE myTable SET Verified=1 WHERE ID=2

你也可以同时做这两个

UPDATE myTable SET Num=3, Verified=1 WHERE ID=2

如果您想保存旧值,您可以轻松地制作一个可以包含时间戳的存档表。然后在运行更新之前将您的值插入到该表中。

注意: Count是错误的列名,因为它是 SQL 中的关键字

于 2013-08-20T17:14:01.987 回答
0

一种方法是,只需创建一个额外的列 STATUS。

一旦用户进入,它可以是 1 状态(表示刚进入) 已批准 - 2 已更改 - 3

虽然 1 和 2 可以在同一行中,但更改的可以是单独的行,在检索时您可以使用 max(status) 检索这些记录,以便每个项目仅获得一条记录,即使它们在数据库中可能有多个 id .

同样的状态逻辑也可以用在其他方面来达到同样的目的。

于 2013-08-20T17:10:32.617 回答