You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于SQL多表关联与条件逻辑计算商品售价的实现需求

商品售价计算SQL需求

我是新手,请多包涵!

需求说明

我需要为商品计算售价,公式为 (CostPrice + 供应商配送费) + 加价(MarkupPrice)。加价规则依价格区间而定,也可按品牌或类别收取附加费替代区间加价。

数据表结构

Distributor表

DistyIDDeliveryCharge
110.00
220.00
38.00
45.00

Stock表

StockIDDistributorIDBrandCategorySKUCostPriceSellPrice
11ApplePhoneABC1225.00
21NokiaPhoneABC34119.00
32SamsungTabletABC35242.00
43PhilipsTVABC56333.50

Markup表

IDBrandCategoryDefaultMarkupMarkupMinPriceMarkupMaxPriceMarkupPrice
1Tablet200.009999.9930.00
2Nokia20100.00599.9920.00
3200.00199.9910.00
420200.00299.9915.00
520300.00399.9920.00

当前现有SQL

目前我有一段SQL仅能关联Stock表与Distributor表计算售价,公式为 Stock.SellPrice = Stock.CostPrice + Distributor.DeliveryCharge:

$sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SET s.SellPrice = s.CostPrice + d.DeliveryCharge;";

需补充的规则

我需要加入Markup表的MarkupPrice,且需遵循以下优先级规则:

  • 优先按品牌附加费计算;
  • 若无品牌规则则按类别附加费计算;
  • 若无类别规则则按价格区间加价计算;
  • 若以上均不满足则使用DefaultMarkup。

示例

  1. 成本价125.00、DistyID为3的商品(无品牌/类别加价):(125.00 + 8.00) + 20.00 = 153.00(售价)
  2. 成本价452.00、DistyID为4的Nokia品牌商品:(452.00 +5.00)+20.00=477.00(售价)

我的初步思路(待完善)

不确定如何关联Markup表与Stock表,也不清楚如何编写符合优先级的条件逻辑,以下是我的初步想法:

If  (Stock.CostPrice <= Markup.MarkupMinPrice) && (s.CostPrice <= MarkupMaxPrice) {
    // BRAND MARKUP
    If {
        Stock.Brand = Markup.Brand 
        $sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SUM (s.CostPrice + d.DeliveryCharge) AS totalcost JOIN MarkupTable m; s.SellPrice = totalcost + m.MarkupPrice;"
    }
    elseif{
        // CATEGORY MARKUP
        Stock.Category = Markup.Category
        $sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SUM (s.CostPrice + d.DeliveryCharge) AS totalcost JOIN MarkupTable m; s.SellPrice = totalcost + m.MarkupPrice;"
    }
    else{
        // STANDARD PRICE MARKUP 
        $sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SUM (s.CostPrice + d.DeliveryCharge) AS totalcost JOIN MarkupTable m; s.SellPrice = totalcost + m.MarkupPrice;"
    }
    else{
        //USE MARKUP.DEFAULT MARKUP 
        $sql = "Update Stock s INNER JOIN Distributor d on s.DistributorID = d.DistyID SUM (s.CostPrice + d.DeliveryCharge) AS totalcost JOIN MarkupTable m; s.SellPrice = totalcost + m.MarkupPrice;"
    }
endif;
}

内容的提问来源于stack exchange,提问作者user18995117

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:02:41