Note that there are some explanatory texts on larger screens.

plurals
  1. POmySql - Best way to model price discount table for bulk purchases - Recursion?
    primarykey
    data
    text
    <p>Building an eCommerce store, and wish to offer pricing discounts for bulk orders. Each product may have different discount tiers.</p> <p>e.g. different % discounts dependent on qty ordered.</p> <pre><code>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%] </code></pre> <p>Bit confused as to how I should model this in a database...</p> <p>My initial thoughts were to model my discounts table as a self refrencing adjacency list. The product table would have a discount_id to determine which discount band it belonged to.</p> <p>e.g.</p> <pre><code>discounts table: id int name string discount decimal parent_id int nullable </code></pre> <p>sample data:</p> <pre><code>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 ============================================= </code></pre> <p>products table</p> <pre><code>id int name string base_price decimal discount_id </code></pre> <p>so in the products table, a product can have a discount_id of either 1 or 5. Lets assume discount_id is 5. So to get a table showing prices and discounts for bulk purchases, I recursively loop through the discount_id's starting from 5 until child_id is null?</p> <p>Am I completely off the mark or is there a better way of modelling this data?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload