建立一个eCommerce商店,并希望为批量订单提供价格折扣。每种产品可能有不同的折扣层级。
例如,不同的%折扣取决于订购的数量。
Product : Quantity Bands[discount]
Product A: 1-10[0%], 11-20[5%], 21-50[15%], 51+[20%]
Product B: 1-5[0%], 6-10[10%], 10+[30%]
Product C: 1-10[0%], 11-20[5%], 21-50[15%], 51+[20%]我不知道该如何在数据库中对此进行建模...
我最初的想法是将我的折扣表建模为一个自重映射邻接列表。product表将有一个discount_id来确定它属于哪个折扣范围。
例如:
discounts table:
id int
name string
discount decimal
parent_id int nullable示例数据:
id name discount child_id
=============================================
1 1-10 0 2
2 11-20 0.05 3
3 21-50 0.15 4
4 51+ 0.20 null
=============================================
5 1-5 0 6
6 6-10 0.10 7
7 10+ 0.30 null
=============================================products表
id int
name string
base_price decimal
discount_id因此,在products表中,产品的折扣可以为1或5。假设discount_id为5。因此,为了获得一个显示批量购买的价格和折扣的表,我递归地循环遍历从5开始的discount_id _id,直到child_id为null?
我是不是完全错了,或者有没有更好的方法来模拟这些数据?
发布于 2016-01-05 22:06:16
您需要一个包含4个字段的表
id product_id层折扣
例如:
1 1 1 0
2 1 11 5
3 1 21 15
4 1 51 20只需将您的products表与order表和discounts表连接起来,即可计算出该产品的折扣
https://stackoverflow.com/questions/19667541
复制相似问题