后续:如何对数据库中的商品进行折扣建模?

发布于 2024-12-22 09:55:54 字数 1628 浏览 1 评论 0原文

我正在建立一个电子商务网站,并希望在有限的时间内为某些商品提供折扣。我想显示我们为每种产品提供多少折扣。因此,我需要每个产品有两个值:原始价格和给定持续时间内的折扣价格。

这是答案的后续内容 问题 我问

架构:

  • 产品

    • 产品ID
    • 姓名
  • 产品定价

    • 产品 ID (FK)
    • 开始日期时间戳
    • 结束日期时间戳
    • 价格
    • 原价仅适用于我们使用方法 A(稍后提供)的情况

数据:

Product:
    1   |   Apple
    2   |   Banana

T1:2011 年 12 月 21 日:目前无交易

ProductPricing
    1   |   Dec 20, 2011, 00:00 |   Jan 1, 2038, 00:00  |   10$ |   10$
    2   |   Dec 20, 2011, 00:00 |   Jan 1, 2038, 00:00  |   20$ |   20$

T2:2011年12月24日:成交!从 12 月 25 日 14:00 至 12 月 26 日 14:00 对苹果应用 25% 的折扣

方法 A - 查询更新给定持续时间内的苹果价格

ProductPricing
    1   |   Dec 25, 2011, 14:00 |   Dec 26, 2011, 14:00 |   7.5$|   10$
    2   |   Dec 20, 2011, 00:00 |   Dec 25, 2038, 00:00 |   20$ |   20$

方法 B. - 查询添加另一条记录,其中包含给定持续时间的苹果价格

ProductPricing
    1   |   Dec 20, 2011, 00:00 |   Jan 1, 2038, 00:00  |   10$ |   10$
    2   |   Dec 20, 2011, 00:00 |   Dec 25, 2038, 00:00 |   20$ |   20$
    1   |   Dec 25, 2011, 14:00 |   Dec 26, 2011, 14:00 |   7.5$|   10$

T3:2011 年 12 月 27 日 - 选项

方法 A。 此时,交易已过期,我是否应该使用触发器重置 endTimeStamp ?

方法B。 我是否应该删除交易刚刚过期的产品的最新记录?

I am building an ecommerce site and would like to offer discounts on certain items for a limited time. I would like to display how much discount we are offering per product. Hence, I need two values per product, original price and the discounted price for the given duration.

This is in followup to an answer for the question I asked

Schema:

  • Product

    • productId
    • Name
  • ProductPricing

    • productId (FK)
    • startDateTimeStamp
    • endDateTimeStamp
    • price
    • original price only applicable if we use approach A (comes later on)

Data:

Product:
    1   |   Apple
    2   |   Banana

T1: Dec 21, 2011: No deals at this time

ProductPricing
    1   |   Dec 20, 2011, 00:00 |   Jan 1, 2038, 00:00  |   10$ |   10$
    2   |   Dec 20, 2011, 00:00 |   Jan 1, 2038, 00:00  |   20$ |   20$

T2: Dec 24, 2011: Deal! Apply discount of 25% on apples from Dec 25, 14:00 - Dec 26, 14:00

Approach A.
- Query updates apple prices for the given duration

ProductPricing
    1   |   Dec 25, 2011, 14:00 |   Dec 26, 2011, 14:00 |   7.5$|   10$
    2   |   Dec 20, 2011, 00:00 |   Dec 25, 2038, 00:00 |   20$ |   20$

Approach B.
- Query adds another record with apple prices for the given duration

ProductPricing
    1   |   Dec 20, 2011, 00:00 |   Jan 1, 2038, 00:00  |   10$ |   10$
    2   |   Dec 20, 2011, 00:00 |   Dec 25, 2038, 00:00 |   20$ |   20$
    1   |   Dec 25, 2011, 14:00 |   Dec 26, 2011, 14:00 |   7.5$|   10$

T3: Dec 27, 2011 - Options

Approach A.
At this time, the deal is expired, should I reset the endTimeStamp using a trigger ?

Approach B.
Should I delete the most recent record for the product for which the deal just expired ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2024-12-29 09:55:54

ProductPricing 表的设计使我们永远不必删除旧的定价数据(有时管理层需要基于该数据的报告)。根据您上面的描述,您可以像这样开始(我更改了开始日期,这样很容易看出,是的,这是系统到位时的原始价格):

ProductPricing
   1   |    Jan 1, 1970, 00:00:00 |   Jan 1, 2038, 00:00:00  |   10$ |   10$

现在假设您给出折扣价在您的苹果上,您希望积极主动地设置销售结束时的系统:

ProductPricing
   1   |    Jan 1, 1970, 00:00:00 |  Dec 20, 2011, 00:00:00  |   10$ |   10$
   1   |   Dec 20, 2011, 00:00:01 |  Dec 26, 2011, 00:00:00  |  7.5$ |   10$
   1   |   Dec 26, 2011, 00:00:01 |   Jan 1, 2038, 00:00:00  |   10$ |   10$

我们在这里所做的是:

  1. 使用 2038 时间戳更新现有记录,更改 endDateTimeStamp 字段以反映的开始sale
  2. 插入一条新记录来定义销售
  3. 插入另一条新记录以再次反映正常价格

由于没有重叠的时间戳,因此当您在数据库中查询价格时,您一定会得到一条记录。因此,

SELECT p.Name, pp.price, pp.original_price
FROM Product p
INNER JOIN ProductPricing pp ON pp.productId = p.productId
WHERE NOW() BETWEEN pp.startDateTimeStamp AND pp.endDateTimeStamp

将为您提供包含当前定价的产品列表。

The design of the ProductPricing table allows us to never have to delete old pricing data (sometimes management wants a report based on that data). With what you have described above, you'd start like this (I changed the starting date just so it's easy to pick out that yes, this was the original price when the system went into place):

ProductPricing
   1   |    Jan 1, 1970, 00:00:00 |   Jan 1, 2038, 00:00:00  |   10$ |   10$

Now let's say you give a discount price on your apples, and you wanted to be proactive and set up the system for when the sale was over:

ProductPricing
   1   |    Jan 1, 1970, 00:00:00 |  Dec 20, 2011, 00:00:00  |   10$ |   10$
   1   |   Dec 20, 2011, 00:00:01 |  Dec 26, 2011, 00:00:00  |  7.5$ |   10$
   1   |   Dec 26, 2011, 00:00:01 |   Jan 1, 2038, 00:00:00  |   10$ |   10$

What we did here was:

  1. Update the existing record with the 2038 timestamp, changing the endDateTimeStamp field to reflect the beginning of the sale
  2. Insert a new record to define the sale
  3. Insert another new record to reflect the normal price again

With no overlapping timestamps, you're guaranteed to get a single record when you query the database for your price. Thus,

SELECT p.Name, pp.price, pp.original_price
FROM Product p
INNER JOIN ProductPricing pp ON pp.productId = p.productId
WHERE NOW() BETWEEN pp.startDateTimeStamp AND pp.endDateTimeStamp

would get you a product list with current pricing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文