2

通过连接到网络服务,我收到了一个数据列表。列表中的每条记录都包含三个类别字段,我将它们保存在带有以下列标记的产品表中:

CategoryName    SubCategoryName    SubSubCategoryName
-----------------------------------------------------
Men             Clothing           Jeans
Women           Jewelry            Bracelets
Women           Clothing           Hoodies
Men             Clothing           Hoodies

产品表:类别名称 | 子类别名称 | 子子类别名称

我想要做的是从产品表中提取类别并将它们保存到具有父/子关系的表中。

Id     ParentId    CategoryName         
-------------------------------
1      NULL        Men
2      1           Clothing
3      2           Jeans
4      NULL        Women
5      4           Jewelry
6      5           Bracelets
7      4           Clothing
8      7           Hoodies
9      2           Hoodies

我可以使用什么 SQL 查询来执行此操作?

4

1 回答 1

1

一、新建表

create table NewCategories (
  ID int IDENTITY(1,1)  primary key,
  ParentID int null,
  Name nvarchar(max)
)

现在,将所有行插入新表(这将分配 ID)

insert into NewCategories (Name) 
select distinct CategoryName
from OldCategories

insert into NewCategories (Name) 
select distinct SubCategoryName 
from OldCategories

insert into NewCategories (Name) 
select distinct SubSubCategoryName
from OldCategories

更新NewCategories表,设置 ParentID 列,一次用于SubCategoryName,一次用于SubSubCategoryName

update nc2
set ParentID = nc1.ID
from NewCategories nc1
   inner join OldCategories oc on oc.CategoryName = nc1.Name
   inner join NewCategories nc2 on oc.SubCategoryName = nc2.Name

update nc2
set ParentID = nc1.ID
from NewCategories nc1
   inner join OldCategories oc on oc.SubCategoryName = nc1.Name
   inner join NewCategories nc2 on oc.SubSubCategoryName = nc2.Name

*CategoryName这假定原始表中没有重复项。

SQL小提琴


对于重复项,您可以执行(稍微复杂一些)

--insert all categories
insert into NewCategories (Name) 
select distinct CategoryName
from OldCategories


--only categories in the "new" table now
insert into NewCategories (ParentID, Name)
select distinct n.ID, o.SubCategoryName 
from OldCategories o
  inner join NewCategories n on o.CategoryName = n.Name

--now subcategories are items with non-null parents, 
-- so we need a double join
insert into NewCategories (ParentID, Name)
select distinct n1.ID, o.SubSubCategoryName 
from OldCategories o
  inner join NewCategories n1 on o.SubCategoryName = n1.Name
  inner join NewCategories n2 on o.CategoryName = n2.Name and n2.ID=n1.ParentID

这是一个新的小提琴,修改为处理重复

于 2013-10-08T09:42:35.477 回答