gpt4 book ai didi

php - Mysql插入当前结果然后插入新结果

转载 作者:行者123 更新时间:2023-11-29 14:49:19 25 4
gpt4 key购买 nike

我有一个ajax实时表编辑来更改我当前产品的价格。我想要做的是在更改之前插入价格,然后插入更新后的价格。原因是因为我想显示更新后价格的变化。示例:当前价格为 54.00 美元,我将其更改为 57.00 美元。我需要记录全天的价格变化并显示 3.00 美元的价格变化。我将如何插入旧价格,同时插入更新的价格。谢谢。

最佳答案

我建议你像这样制定价格表

table price
-----------
id unsigned integer autoincrement primary key
article_id integer /*link to articletable*/
valid_from date
valid_until date
amount decimal(10,2) /*always use decimal for money*/

然后您可以使用以下 4 个查询插入新价格。

/*hide changes from the rest of the world until we are done*/
START TRANSACTION

/*invalidate the latest existing price in the price table*/
UPDATE price
SET valid_until = DATESUB(CURDATE(),INTERVAL 1 DAY)
WHERE article_id = '100' ORDER BY valid_until DESC LIMIT 1
/*the order by selects the latest item, the limit does only 1 update*/

/*insert the new price*/
INSERT INTO PRICE (article_id, valid_from, valid_until, amount)
VALUES ('100', CURDATE(), DATEADD(CURDATE(),INTERVAL 100 YEAR), '99.95')

/*show changes to the rest of the world*/
COMMIT

您需要进行交易,否则您将面临价格表不同步的风险。在价格表上将表类型设置为 InnoDB。所有其他表都可以是 MyISAM,只需确保 price 表是 InnoDB

您现在可以使用以下方式选择价格:

SELECT article.name
,price.amount as price_per_item
,purchase.qty as number_of_items
,price.amount * purchase.qty as amount
FROM purchase
INNER JOIN article ON (article.id = purchase.article_id)
INNER JOIN price ON (price.article_id = purchase.article_id)
AND (purchase.transactiondate BETWEEN price.valid_from and price.valid_until)
WHERE purchase.id = '458'

关于php - Mysql插入当前结果然后插入新结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6141335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com