how to model discount on items in a database?

前端 未结 4 2074
离开以前
离开以前 2021-02-05 23:30

I am building an ecommerce site and would like to offer discounts on certain items for a limited time.

My Product table (MySQL) looks like this:

Product
    -         


        
相关标签:
4条回答
  • 2021-02-06 00:02

    I would use a decimal for the discount column.

    price * discount = $amount off

    price - $amount off = price in cart

    For limited time you could put in a expiration date column, and only return rows that are not expired.

    0 讨论(0)
  • 2021-02-06 00:10

    Another thing to consider would be, how do you model situation when there is no discount for a given item? You might use a Null Object pattern here - basically, when a product is created, you also add a deal on that product with 0% discount and unlimited time. That way, you could simplify your product retrieval logic (no outer joins in query, no if for calculating price).

    0 讨论(0)
  • 2021-02-06 00:11

    You might consider adding a beginning timestamp and ending timestamp to your Deals table. That way, you can check to make sure the current date is between the start and end dates for the deal.

    Your Deals table doesn't really need a dealID - it could be keyed with the productID and the start date of the discount. Also, depending on how high the price could be for a given item, remember to make your discount field something sufficiently precise (something like DECIMAL 12,8).

    If it were me, I would actually leave price off of the Product table and create a ProductPricing table instead of creating a Deals table. That ProductPricing table would consist of the productID and a starting timestamp as the key fields, then also have an ending timestamp to indicate when that price changed. And, of course, the price of the item.

    0 讨论(0)
  • 2021-02-06 00:19

    I would add discountPrice,discountPercentage,endDiscountDate column to table Product and create table discount history to keep discount tracking

    Then when select no need to join table you can choose the correct price by checking endDiscountDate

    0 讨论(0)
提交回复
热议问题