一种解决方案(性能垃圾,基本实现功能)

商品表  属性集表 属性表 属性值表 SKU表 SKU选项表  属性集和属性之间的中间表
表关系
商品表  *--------------1  属性集表
属性集表 *------------------* 属性表
属性表 1 --------------* 属性值表
商品表  1----------------* SKU
SKU 1-------* SKU选项{理解成多对多,会麻烦;需要把所有的可能组合写到选项中之后创建中间表}

商品表

CREATE TABLE Products
(
[ID] [int] IDENTITY(1,1) NOT NULL,--主键
[Name] [nvarchar](50) NOT NULL,--名称
[Describe] [nvarchar](max) NULL,--描述
[PropertiesSetID] [int] NOT NULL--属性集id
) 

属性集合表(属性的组合,便于选择属性并选择属性对应的值)

CREATE TABLE PropertiesSet
(
[ID] [int] IDENTITY(1,1) NOT NULL,--主键

[Describe] [nvarchar](max) NULL--描述
) 

属性表 

CREATE TABLE Property
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Describe] [nvarchar](max) NULL
) 

属性值表

 
CREATE TABLE PropertyValue
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Value] [nvarchar](50) NOT NULL,

[PropertyID] [int] NOT NULL,


属性集和属性中间表

CREATE TABLE SetBtProperty
(
[SetBtPropertiesID] [int] IDENTITY(1,1) NOT NULL,
[SetID] [int] NOT NULL,

[PropertyID] [int] NOT NULL,
) 

 SKU表

CREATE TABLE  SKU
(
[SKUID] [nvarchar](128) NOT NULL,
[Price] [money] NOT NULL,
[Number] [int] NOT NULL,

[ProductID] [int] NOT NULL,


SKU选项表

CREATE TABLE  SKUOptions
(
[SKUID] [nvarchar](128) NOT NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[PropertyID] [int] NOT NULL,

[PropertyValueID] [int] NOT NULL


商家添加商品
商家会填入一些信息
商品名称,再通过 属性集合来便捷选择该商品需要的属性 ,商家选择各个属性对应的值
传递给sqlserver添加商品

商家可以添加属性集合

create procedure [dbo].[sp_CreatePropertySet]
(
@PropertyIDS nvarchar(512),--属性的参数集合 通过 ID;ID的方式进行拼接的字符串
@SetDescribe nvarchar(512)--描述要创建的 属性集合
)
as 
begin--这里这样可能没有直接 插入库简便,为了练习 表变量和遍历
 
 
insert into PropertiesSet (Describe) values (@SetDescribe) --添加属性集 PropertySet 
declare @setID int ; set @setID=@@IDENTITY--表示刚插入的集合的id
 
 
--sqlserver中没有集合List的概念,但是有些时候需要用到 类似于【集合,数组】的功能,比如这个@PropertyIDS,通过 ID;ID的方式进行拼接的字符串 需要进行 ;切割获取到 每个属性的ID,之后在需要的地方还需要取出来使用  在c#中能够使用List;sqlserver中可以通过【表变量】来实现集合和数组的功能 
 
--截取属性集字符串放入【“数组”】
declare @StorePropertyIDS table --声明存储 PropertyID的表变量
(
id int,--模拟数组集合的索引 0 1 2。。。
PropertyID int--值
)
 
declare @conditionNum int
set @conditionNum=-1
 
while(@conditionNum<>0)--charindex得不到就返回0,不同于c#返回-1
begin
declare @location int 
        set @location = charindex(';',@PropertyIDS)--存储;出现的位置 12;123;23568
        if(@location<=1)
begin
declare @str nvarchar(512) = substring(@PropertyIDS,1,len(@PropertyIDS))
 
 insert into @StorePropertyIDS (id,PropertyID) values (@conditionNum+1,cast(@str as int))
  set @PropertyIDS= substring(@PropertyIDS,@location+1,len(@PropertyIDS)-@location)--重新赋值 ids组合字符串
 
end
else--》1
begin
declare @str2 nvarchar(512)=SUBSTRING(@PropertyIDS,1,@location-1)
 
insert into @StorePropertyIDS (id,PropertyID) values (@conditionNum+1,cast(@str2 as int))
set @PropertyIDS= substring(@PropertyIDS,@location+1,len(@PropertyIDS)-@location)--重新赋值 ids组合字符串
end
       
        set @conditionNum = @location--while的条件使用,当charindex没有的时候不不需要再循环了
 
 
 
--遍历表变量插入 中间表
--sqlserver中遍历一个表的方法
      declare @id int;declare @ProID int;declare @nec bit;
  while exists(select id from @StorePropertyIDS)
begin
set rowcount 1
select @id = id,@ProID=PropertyID from @StorePropertyIDS
set rowcount 0
delete from @StorePropertyIDS where id = @id
 
insert into SetBtProperty (SetID,PropertyID) values(@setID,@ProID)
end
 
end
 
end

开始添加商品

create procedure [dbo].[sp_AddProduct]
(
@num int,--数量
@price money,--价格
@productID int,--商品id
@setID int,--属性集合id
@options nvarchar(612),--属性;值得组合, 如 颜色id#对应的值id;想好id#对应的值id
@name nvarchar(168),--商品名称
@describe nvarchar(614)--商品描述
)
as 
begin--{
declare @pidExists int;
select @pidExists = count(1) from Products where ID=@productID
if(@pidExists=0)--不存在
begin
insert into Products(Name,Describe,PropertiesSetID) values (@name,@describe,@setID)
set @productID = @@IDENTITY
end
 
 
declare @pv table(id int identity(1,1),propID int,pvalue int)--声明表变量,把截取得到的属性编号和值编号存起来,类似于代码中的集合的作用List
declare @pv2 table(id int identity(1,1),propID int,pvalue int)--由于遍历一次要删掉,要用2次就声明了两个
declare @tb table(id int identity(1,1),skuid nvarchar(128))
--获取属性值得组合,属性的个数等在代码中进行控制  [1#1;2#5;13#23]
declare @whileNum int=-1
 
--截取@options插入到表变量@pv
while (@whileNum<>0)
begin--{
declare @kv nvarchar(128) --存储截取后的前部分的字符串
declare @location int = charindex(N';',@options)--表示;在options中的位置,sqlserver中的位置是从1开始计算的
if(@location=0)--没有了;
begin--{
set @kv =  substring(@options,1,len(@options))
insert into @pv(propID,pvalue) values (cast(SUBSTRING(@kv,1,charindex(N'#',@kv)-1) as int),SUBSTRING(@kv,charindex(N'#',@kv)+1,len(@kv)-charindex(N'#',@kv)))--插入@pv表变量
insert into @pv2(propID,pvalue) values (cast(SUBSTRING(@kv,1,charindex(N'#',@kv)-1) as int),SUBSTRING(@kv,charindex(N'#',@kv)+1,len(@kv)-charindex(N'#',@kv)))--插入@pv2表变量
end--}
else--能够截取到;
begin--{
set @kv = substring(@options,1,@location-1)
insert into @pv(propID,pvalue) values (cast(SUBSTRING(@kv,1,charindex(N'#',@kv)-1) as int),SUBSTRING(@kv,charindex(N'#',@kv)+1,len(@kv)-charindex(N'#',@kv)))--插入@pv表变量
insert into @pv2(propID,pvalue) values (cast(SUBSTRING(@kv,1,charindex(N'#',@kv)-1) as int),SUBSTRING(@kv,charindex(N'#',@kv)+1,len(@kv)-charindex(N'#',@kv)))--插入@pv2表变量
    set @options = SUBSTRING(@options,@location+1,len(@options)-@location);--重新个options赋值
end--}
set @whileNum = @location --当没有;时候charindex()返回0
end--}
 
declare @sqlStr nvarchar(1000)=''--用来根据id+属性+值获取sku信息的语句
--根据商品id+属性+属性值 来获取skuid,是否存在,存在则修改;不存则添加新的skuid
declare @id int;declare @propID int;declare @pvalue int
while exists(select id from @pv)--遍历白表变量
begin--{
set rowcount 1
select @id=id,@propID=propid,@pvalue=pvalue from @pv
set rowcount 0
delete from @pv where id = @id
set @sqlStr =@sqlStr+ N'select skuid from SKUOptions where PropertyID ='+cast(@propID as nvarchar(32))+N' and PropertyValueID ='+cast(@pvalue as nvarchar(32))+' intersect '
end--}
set @sqlStr = substring(@sqlStr,1,len(@sqlStr)-10)--把最后的intersect去掉
insert into @tb exec sp_executesql @sqlStr--把sp_executesql得到的结果集插入表变量@tb中
 
declare @HaveSKUID nvarchar(128);
select @HaveSKUID = skuid from SKU where ProductID=@productID and skuid in (select skuid from @tb)
 
if(@HaveSKUID is null)--不存在
begin--{
declare @newid nvarchar(128)=NEWID()
insert into SKU (SKUID,Price,Number,ProductID) values (@newid,@price,@num,@productID)--插入sku表
--遍历pv2插入skuoption表
 
while exists(select id from @pv2)
begin--{
set rowcount 1
    select @id=id,@propID=propid,@pvalue=pvalue from @pv2
    set rowcount 0
    delete from @pv2 where id = @id
insert into SKUOptions(SKUID,PropertyID,PropertyValueID) values(@newid,@propID,@pvalue)--插入skuoption表
end--}
end--}
else--存在
begin--{
update SKU set Number=Number+@num where SKUID=@HaveSKUID--只需要修改数量即可
end--}
 
end--}

加入购物车,此时 能够得到的参数是 商品id  买家选该商品的属性和值

 
create proc [dbo].[sp_GetSKUFromPropIdAndProperty]--根据商品id和属性获取sku信息,根据skuid的情况一般的sql就能够满足
(
@productID int,--商品id
@options nvarchar(612)--属性;值得组合, 如 颜色id#对应的值id;想好id#对应的值id
 
)
as
begin--{
declare @pv table(id int identity(1,1),propID int,pvalue int)--声明表变量,把截取得到的属性编号和值编号存起来,类似于代码中的集合的作用List
declare @tb table(id int identity(1,1),skuid nvarchar(128))--声明表变量,存储sku信息
declare @whileNum int=-1
--截取@options插入到表变量@pv
while (@whileNum<>0)
begin--{
declare @kv nvarchar(128) --存储截取后的前部分的字符串
declare @location int = charindex(N';',@options)--表示;在options中的位置,sqlserver中的位置是从1开始计算的
if(@location=0)--没有了;
begin--{
set @kv =  substring(@options,1,len(@options))
insert into @pv(propID,pvalue) values (cast(SUBSTRING(@kv,1,charindex(N'#',@kv)-1) as int),SUBSTRING(@kv,charindex(N'#',@kv)+1,len(@kv)-charindex(N'#',@kv)))--插入@pv表变量
 
end--}
else--能够截取到;
begin--{
set @kv = substring(@options,1,@location-1)
insert into @pv(propID,pvalue) values (cast(SUBSTRING(@kv,1,charindex(N'#',@kv)-1) as int),SUBSTRING(@kv,charindex(N'#',@kv)+1,len(@kv)-charindex(N'#',@kv)))--插入@pv表变量
 
    set @options = SUBSTRING(@options,@location+1,len(@options)-@location);--重新个options赋值
end--}
set @whileNum = @location --当没有;时候charindex()返回0
end--}
 
declare @sqlStr nvarchar(1000)=''--用来根据id+属性+值获取sku信息的语句
--根据商品id+属性+属性值 来获取skuid,是否存在,存在则修改;不存则添加新的skuid
declare @id int;declare @propID int;declare @pvalue int
while exists(select id from @pv)--遍历白表变量
begin--{
set rowcount 1
select @id=id,@propID=propid,@pvalue=pvalue from @pv
set rowcount 0
delete from @pv where id = @id
set @sqlStr =@sqlStr+ N'select skuid from SKUOptions where PropertyID ='+cast(@propID as nvarchar(32))+N' and PropertyValueID ='+cast(@pvalue as nvarchar(32))+' intersect '
end--}
set @sqlStr = substring(@sqlStr,1,len(@sqlStr)-10)--把最后的intersect去掉
insert into @tb exec sp_executesql @sqlStr--把sp_executesql得到的结果集插入表变量@tb中
select * from SKU where ProductID=@productID and skuid in (select skuid from @tb)

end--} 

当然购物车中或者其他地方能够得到skuid,直接通过他查询即可,直接where查询完事..

商品库存SKU的更多相关文章

  1. Newbe.Claptrap 框架入门,第三步 —— 定义 Claptrap,管理商品库存

    接上一篇 Newbe.Claptrap 框架入门,第二步 —— 简单业务,清空购物车 ,我们继续要了解一下如何使用 Newbe.Claptrap 框架开发业务.通过本篇阅读,您便可以开始学会添加一个全 ...

  2. B2C电子商务系统研发——商品SKU分析和设计(二)

    转:http://www.cnblogs.com/winstonyan/archive/2012/01/07/2315886.html 上文谈到5种商品SKU设计模式,本文将做些细化说明. 笔者研究过 ...

  3. 商品sku规格选择效果,没有商品的不能选中,选择顺序不影响展示结果

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  4. 淘宝SKU组合查询算法实现

    淘宝SKU组合查询算法实现 2015-11-14 16:18 1140人阅读 评论(0) 收藏 举报  分类: JavaScript(14)    目录(?)[+]   前端有多少事情可以做,能做到多 ...

  5. SPU和SKU有什么区别

    SPU = Standard Product Unit (标准产品单位)SPU是商品信息聚合的最小单位,是一组可复用.易检索的标准化信息的集合,该集合描述了一个产品的特性.通俗点讲,属性值.特性相同的 ...

  6. cmd 执行Dcpromo错误:在该 SKU 上不支持 Active Directory 域服务安装向导,Windows Server 2008 R2 Enterprise 配置AD(Active Directory)域控制器

    今天,要安装AD域控制器,运行dcpromo结果提示:在该 SKU 上不支持 Active Directory 域服务安装向导. 以前弄的时候直接就通过了,这次咋回事?终于搞了大半天搞定了. 主要原因 ...

  7. Magento2 可配置产品解决SKU流程

    选择可配置产品: 填写必填信息与库存 创建配置 执行四步后完成创建:4.1:选择需要的规格属性: 4.2:选择组合需要的属性值:4.3:根据您的选择,将创建3个新产品.使用此步骤自定义新产品的图像和价 ...

  8. SKU : Stock Keeping Unit

    Stock Keeping Unit  is a number assigned to a product by a retail store to identify the price, produ ...

  9. 保密数据!泽宝曝光各个主要店铺收入 核心SKU数量少得惊人

    今年跨境电商圈的一大并购,上市公司星徽精密并购知名跨境电商大卖家泽宝股份正在进程中.星徽精密在向证监会行政许可项目审查回复中,披露了泽宝股份众多保密数据,揭开了泽宝股份众多经营关键点,值得跨境电商卖家 ...

随机推荐

  1. Scala的基本语法总结

    Scala的函数: 目前博客园中的代码编辑器中还不支持Scala语言....用的Java代码的存储方式 object TestScala { def main(args: Array[String]) ...

  2. Android开发需要注意的坑

    Android开发需要注意的坑一览​对于一些Android开发过程中坑爹.细小,但又重要的错误的总结​Android开发在路上:少去踩坑,多走捷径其他参考: ​google官方版本发布图 ​umeng ...

  3. hihocoder 1237 Farthest Point

    #1237 : Farthest Point 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 Given a circle on a two-dimentional pla ...

  4. poj 3544 Journey with Pigs

    Journey with Pigs Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3004   Accepted: 922 ...

  5. SAX - Hello World

    SAX 是一种事件驱动的 XML 数据处理模型.对于 DOM 模型,解析 XML 文档时,需要将所有内容载入内容.相比 DOM 模型,SAX 模型更为高效,它一边扫描一边解析 XML 文档.但与 DO ...

  6. Android之图片应用

    package com.example.imagescale; import android.os.Bundle; import android.app.Activity; import androi ...

  7. Agile.Net 组件式开发平台 - 组件开发示例

    所谓组件式开发平台,它所有的功能模块都是以组件的形式扩展的,下面我来演示一个简单的组件开发例程. Agile.Net开发管理平台项目,已经托管在开源中国码云平台(http://git.oschina. ...

  8. 搭建私有git代码托管服务就是这么简单(简单5步)

    部署一个git代码托管服务就是这么简单 --基于阿里云ecs以docker容器运行gogs代码托管服务 部署步骤: 1.新建ecs云主机,选定操作系统为ubuntu 12.4tls 2.搭建docke ...

  9. mvc Web api 如何在控制器中调用

    关于如何调用 mvc Web api 的方法,网上一搜就是一大把,基本都是在前台jq中调用的,但是如何在后台调用呢? 本楼主做了一下测试,仅供参考. 先写一个简单的api,如下:[域1] namesp ...

  10. CentOS6.3系统安装SCP命令

    原文:http://www.111cn.net/sys/CentOS/58387.htm CP使用SSH协议在Linux系统中进行文件传输,但我最小安装的CentOS 6.3没有该命令.  代码如下 ...