gpt4 book ai didi

sql - 最低价选择

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:33:10 24 4
gpt4 key购买 nike

情况是这样的:

我们有产品“A123”,我们必须记住它的最低价格。一种产品的价格来自随机数量的商店,无法判断商店 x 何时向我们发送“A123”的价格。

所以我有带列的 SQL 表:

product_number
price
shop (from which shop this price comes)

用于更新产品价格的 SQL 函数如下所示(这是 SQL 伪代码,语法无关紧要):

function update_product(in_shop, in_product_number, in_price)
select price, shop into productRow from products where product_number = in_product_number;
if found then
if (productRow.price > in_price) or (productRow.price < in_price and productRow.shop = in_shop) then
update row with new price and new shop
end if;
else
insert new product that we didn't have before
end if;

(productRow.price < in_price and productRow.shop = in_shop) 条件是为了防止出现这种情况:

在产品表中我们有

A123 22.5 amazon

然后又是来自亚马逊的信息:

A123 25 amazon

由于上述情况,我们将价格更新为更高的正确行为。

但算法在这种情况下失败了:我们在产品表中再次有一行:

A123 22.5 amazon

然后是来自 merlin 的信息

A123 23 merlin (we don't update because price is higher)

然后是来自亚马逊的信息

A123 35 amazon

然后我们更新表,现在我们有:

A123 35 amazon

但这是错误的,因为 merlin 早些时候对该产品的价格较低。

知道如何避免这种情况吗?

最佳答案

解决问题的唯一方法是跟踪每家商店的价格,然后只返回最低的当前价格。因此,例如,您需要一张与已有表格类似的表格,但是当您从表格中选择如下内容时:

select min(price)
from products
where product_number = :my_product

就我个人而言,我会保留您何时收到产品价格更新的时间戳,以便您能够确定何时收到更新。

关于sql - 最低价选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1920204/

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