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

商品表  属性集表 属性表 属性值表 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. 【程序员的SQL金典】笔记(第6章~第11章)

        第六章 索引与约束   1.索引用来提高数据的检索速度,而约束则用来保证数据的完整性.   2.创建索引 创建索引的SQL 语句是CREATE INDEX,其语法如下: CREATE INDE ...

  2. php5.4window下连接mssql

    第一步 下载 http://www.microsoft.com/en-us/download/details.aspx?id=20098 [PHP_PDO_SQLSRV] extension=php_ ...

  3. PYTHON:HTTP头设置工具(以附件名为例)

    import oss2 # 用户授权 auth = oss2.Auth('accessId', 'accessKey')# oss bucket bucket = oss2.Bucket(auth, ...

  4. Linux c字符串中不可打印字符转换成16进制

    本文由 www.169it.com 搜集整理 如果一个C字符串中同时包含可打印和不可打印的字符,如果想将这个字符串写入文件,同时方便打开文件查看或者在控制台中打印出来不会出现乱码,那么可以将字符串中的 ...

  5. Swift静态属性

    在介绍静态属性之前,我们先来看一个类的设计,有一个Account(银行账户)类,假设它有3个属性:amount(账户金额).interestRate(利率)和owner(账户名).在这3个属性中,am ...

  6. IOS 解析JSON

    - (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, t ...

  7. 为什么要使用jQuery?

    首先必须得了解为什么要学习JQuery,JQuery有哪些优点,当然是相对于传统的Javascript和DOM来说了,现在将JQuery的优势总结如下: 1,轻量级. JQuery非常小,压缩包只有1 ...

  8. (转)推荐一个在Linux/Unix上架设ASP.NET的 WEB服务器--Jexus

    在Linux/Unix上架设ASP.NET WEB服务器,有两个可选方式,一种是Mono+XSP,一种是Mono+Jexus,其它的方式,比如 Apache+mod_mono.Nginx+FastCg ...

  9. echarts标准饼图解读(一)——提示框(tooltip)配置

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

  10. 4月8日学习笔记(js基础)

    <script>标签放在<body>和<head> 放在 <head></head> 里的会比放在 <body></bod ...