商品 分类 属性 表设计 库存

本文深入探讨了商品分类、属性、规格等关键概念及其在电商系统中的应用,包括数据库设计、缓存策略和前端展示逻辑。主要内容涵盖商品分类表、属性表、规格表的构建,以及如何通过数据库查询实现高效的商品搜索与筛选功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据不同的商品分类选择,特殊商品出现不同 扩展属性/规格,如:衣服出现尺码,颜色。cate_tree字段可以不用存储到数据库,将列表直接存储redis等通过id,pid遍历也很快

private function getCategoryTreeAndLevel($id, $parent_id) {
    $level = 1;
    $current_cate_tree = $id;
    if ($parent_id > 0) {
        $pInfo = $this->getInfo($parent_id);
        if ($pInfo) {
            $current_cate_tree = "{$pInfo['cate_tree']}-{$current_cate_tree}";
            $level = $pInfo['level'] + 1;
        }
    }
    return ['cate_tree' => $current_cate_tree, 'level' => $level];
}

模糊查询

 (cate_tree LIKE 'pid-%' or cate_tree LIKE '%-pid-%')

 1.商品分类表:

一次性把数据读出来(id做key)然后递归处理,不要递归取数据库。缓存起来

Array
(
    [758] => Array
        (
            [id] => 758
            [name] => 综合开单
            [pid] => 99
        )

    [738] => Array
        (
            [id] => 738
            [name] => 商家首页
            [pid] => 92
        )

CREATE TABLE `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '分类ID',
  `pid` int(11) DEFAULT '0' COMMENT '分类父ID',
  `level` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '层数',
  `name` varchar(100) DEFAULT NULL COMMENT '分类名称',
  `seo_title` varchar(20) DEFAULT NULL COMMENT 'SEO标题',
  `seo_keyword` varchar(20) DEFAULT NULL COMMENT 'SEO关键字',
  `seo_desc` varchar(50) DEFAULT NULL COMMENT 'SEO描述',
  `cate_tree` varchar(100) DEFAULT NULL COMMENT '分类树形结构',
  `listorder` int(11) DEFAULT '0' COMMENT '排序',
  PRIMARY KEY (`id`),
  KEY `pid` (`pid`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;

INSERT INTO `category` (`id`, `pid`, `level`, `name`, `seo_title`, `seo_keyword`, `seo_desc`, `cate_tree`, `listorder`)
VALUES
(1, 0, 1, '日常所需', '', '', '', '1', 0),
(2, 1, 2, '注册公司', '', '', '', '1-2', 0),
(3, 2, 3, '国内游', '', '', '', '1-2-3', 0),
(4, 3, 4, '上海游', '', '', '', '1-2-3-4', 0),
(5, 1, 2, '企业开户', '', '', '', '1-5', 0),
(6, 0, 1, '办公硬件', '', '', '', '6', 0);

分隔符要用 ‘-’,不要用下划线 ‘_’

2.分类属性表(核心)

CREATE TABLE `category_fields` (
  `field_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '分类属性表',
  `category_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '分类ID',
  `field_name` varchar(100) NOT NULL DEFAULT '' COMMENT '属性code',
  `title` varchar(255) NOT NULL DEFAULT '' COMMENT '名称',
  `description` varchar(255) NOT NULL DEFAULT '' COMMENT '描叙',
  `required` enum('no','yes') NOT NULL DEFAULT 'no' COMMENT '是否必填',
  `formtype` varchar(255) NOT NULL DEFAULT '' COMMENT '表单类型',
  `choices` text NOT NULL COMMENT '值选项',
  `sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序',
  `unit` varchar(10) DEFAULT NULL COMMENT '单位',
  `min` int(11) unsigned DEFAULT NULL COMMENT '文本框时的最小值',
  `max` int(11) unsigned DEFAULT NULL COMMENT '文本框时的最大值',
  PRIMARY KEY (`field_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

INSERT INTO `category_fields` (`field_id`, `category_id`, `field_name`, `title`, `description`, `required`, `formtype`, `choices`, `sort`, `unit`, `min`, `max`)
VALUES
(1, 1, 'renjunyusuan', '人均预算', '', 'no', 'text', '', 0, '元', 1, 2),
(2, 1, 'renshu', '旅游人数', '', 'no', 'text', '', 0, '', NULL, NULL),
(3, 1, 'daogou', '允许导购', '', 'no', 'radio', '是 否', 0, '', NULL, NULL),
(4, 1, 'days', '预计天数', '', 'no', 'checkbox', '2天1夜 3天2夜', 0, '', NULL, NULL);


 3.商品属性值表

CREATE TABLE `mall_product_field_value` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品属性值',
  `product_id` int(11) NOT NULL DEFAULT '0' COMMENT '商品ID',
  `field_id` int(11) NOT NULL DEFAULT '0' COMMENT '属性ID',
  `value` text COMMENT '属性值',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

二规格表

CREATE TABLE `catalog_standard` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '分类规格表',
  `pid` int(11) NOT NULL DEFAULT '0',
  `level` int(11) NOT NULL DEFAULT '1',
  `catalog_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '分类ID',
  `name` varchar(100) NOT NULL DEFAULT '' COMMENT '规格code',
  `value` varchar(100) NOT NULL DEFAULT '' COMMENT '值选项',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

INSERT INTO `catalog_standard` (`id`, `pid`, `level`, `catalog_id`, `name`, `value`)
VALUES
(1, 0, 1, 35, 'color', '颜色'),
(2, 0, 1, 35, 'size', '尺码'),
(3, 1, 2, 35, 'red', '红色'),
(4, 1, 2, 35, 'black', '黑色'),
(5, 2, 2, 35, 'S', 'S'),
(6, 2, 2, 35, 'M', 'M'),
(7, 2, 2, 35, 'L', 'L');

规格checkbox勾选值表

CREATE TABLE `mall_product_standard_value` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品规格值',
  `product_id` int(11) NOT NULL DEFAULT '0' COMMENT '产品ID',
  `root_standard_id` int(11) NOT NULL DEFAULT '0' COMMENT 'level=1的StandardID',
  `standard_id` varchar(100) NOT NULL DEFAULT '0' COMMENT '规格ID',
  `value` varchar(100) NOT NULL DEFAULT '' COMMENT '值',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO `mall_product_standard_value` (`id`, `product_id`, `root_standard_id`, `standard_id`, `value`)
VALUES
(1, 1, 1, '3', '红色1'),
(2, 1, 1, '4', '黑色'),
(3, 1, 2, '5', 'S222')

规格勾选后,js生成不同规格组合的表单(价格/库存/货号),修改勾选规格名称,表单的规格名称也随之变化商品的基础属性库存不允许填写,库存是自动加和的

 

不同规则选项组合表

CREATE TABLE `mall_product_standard_value_group` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品规则组合值',
    `product_id` int(11) NOT NULL DEFAULT '0' COMMENT '产品ID',
    `standard_ids` varchar(100) DEFAULT NULL COMMENT '规格组合standard_value_ids',
    `inventory` int(11) NOT NULL DEFAULT '0' COMMENT '库存',
    `price` int(11) NOT NULL DEFAULT '0.00' COMMENT '价格(分)',
    `sku` varchar(60) DEFAULT NULL COMMENT '货号',
    `bar_code` varchar(32) DEFAULT NULL COMMENT '商品条形码',
    PRIMARY KEY (`id`),
    UNIQUE KEY `standard_group` (`standard_ids`,`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

数组排列组合

<script>
    function test() {
        var temparr = [
            ["M", "XL"],
            ["红色", "白色"],
            ["长", "短"],
            ["1", "2"],
            ["A", "B"],
            ["无", "有"]
        ];
        var ret = doExchange(temparr);
        document.write("共有:" + ret.length + "种组合!<br/>");
        for (var i = 0; i < ret.length; i++) {
            document.write(ret[i] + "<br/>");
        }
    }
    function doExchange(doubleArrays) {
        var len = doubleArrays.length;
        if (len >= 2) {
            var len1 = doubleArrays[0].length;
            var len2 = doubleArrays[1].length;
            var newlen = len1 * len2;
            var temp = new Array(newlen);
            var index = 0;
            for (var i = 0; i < len1; i++) {
                for (var j = 0; j < len2; j++) {
                    temp[index] = doubleArrays[0][i] + ',' + doubleArrays[1][j];
                    index++;
                }
            }
            var newArray = new Array(len - 1);
            for (var i = 2; i < len; i++) {
                newArray[i - 1] = doubleArrays[i];
            }
            newArray[0] = temp;
            return doExchange(newArray);
        } else if (len == 1) {
            return doubleArrays[0];
        } else {
            return doubleArrays;
        }
    }
    test();
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值