2

我有两张表,一张是物品容器,它包含与产品相关的所有数据。另一个表用于类别,其中数据使用层次结构进行组织。
查询的目标是从所选类别向下列出与类别匹配的项目。

例子:

CatID: 3, Parent: Root, Name: Computers  
CatID: 4, Parent: 3, Name: Laptops  
CatID: 5, Parent: 3, Name: Monitors  
CatID: 6, Parent: 3, Name: Printers  
CatID: 7, Parent: 6, Name: Laser  
CatID: 8, Parent: 6, Name: Ink  
CatID: 9, Parent: 6, Name: Multifunction  
CatID: 10, Parent: 6, Name: Copier  

如果CatID选择3,则显示所有计算机产品,但如果CatID选择6,则仅显示打印机(带有CatID7、8、9和10的项目)

我对层次结构 ID 很陌生,我对如何处理这个查询一无所知。

感谢所有做出贡献的人。

卡洛斯

两个表的模式 想法是将 mz_category 连接到 ic_item_cat,因此查询可以列出给定类别的所有项目,也可以列出所有子类别。这适用于特殊的“自制”树视图,其中所有类别都根据层次结构显示;当用户单击一个类别时,它会显示属于该类别的所有项目以及属于子类别的所有项目。希望你能明白。

/****** Object:  Table [dbo].[mz_category]    Script Date: 04/26/2012 19:14:34 ******/
SET ARITHABORT ON
GO
SET CONCAT_NULL_YIELDS_NULL ON
GO
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
SET ANSI_WARNINGS ON
GO
SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
SET ARITHABORT ON
GO
/****** Categories Table ******/
CREATE TABLE [dbo].[mz_category](
    [CatNode] [hierarchyid] NOT NULL,
    [CatLevel]  AS ([CatNode].[GetLevel]()),
    [CatID] [int] NOT NULL,
    [CatName] [varchar](80) NOT NULL,
    [SectorId] [varchar](2) NULL,
    [CatIcon] [varchar](255) NULL,
    [oldCat] [varchar](8) NULL,
PRIMARY KEY CLUSTERED 
(
    [CatNode] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
    [CatID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[ic_item_cat]    Script Date: 04/26/2012 19:14:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
/****** Items Table ******/
CREATE TABLE [dbo].[ic_item_cat](
    [item_code] [varchar](25) NOT NULL,
    [item_description] [varchar](80) NULL,
    [item_sector] [varchar](2) NULL,
    [item_line] [varchar](2) NULL,
    [item_reference] [varchar](50) NULL,
    [item_upcean] [varchar](50) NULL,
    [item_category] [int] NULL,
    [item_brand] [varchar](8) NULL,
    [item_cost] [decimal](12, 4) NULL,
    [item_price1] [decimal](12, 4) NULL,
    [item_price2] [decimal](12, 4) NULL,
    [item_price3] [decimal](12, 4) NULL,
    [item_webprice] [decimal](12, 4) NULL,
    [item_dprice1] [decimal](12, 4) NULL,
    [item_dprice2] [decimal](12, 4) NULL,
    [item_dprice3] [decimal](12, 4) NULL,
    [item_lastcost] [decimal](12, 4) NULL,
    [item_lastcostdate] [datetime] NULL,
    [item_lastqtyout] [decimal](12, 4) NULL,
    [item_lastqtyoutdate] [datetime] NULL,
    [item_lastqtyin] [decimal](12, 4) NULL,
    [item_lastqtyindate] [datetime] NULL,
    [item_additionaldesc] [varchar](max) NULL,
    [item_weight] [decimal](12, 4) NULL,
    [item_weight_measure] [varchar](2) NULL,
    [item_width] [decimal](12, 4) NULL,
    [item_width_measure] [varchar](2) NULL,
    [item_length] [decimal](12, 4) NULL,
    [item_length_measure] [varchar](2) NULL,
    [item_height] [decimal](12, 4) NULL,
    [item_height_measure] [varchar](2) NULL,
    [item_whpackdesc] [varchar](80) NULL,
    [item_salespackdesc] [varchar](80) NULL,
    [item_purchpackdesc] [varchar](80) NULL,
    [item_salespackconv] [decimal](12, 4) NULL,
    [item_purchpackconv] [decimal](12, 4) NULL,
    [item_warranty] [varchar](2) NULL,
    [item_delivtime] [varchar](2) NULL,
    [item_rating] [int] NULL,
    [item_vat] [varchar](2) NULL,
    [item_status] [int] NULL,
    [item_avgleadtime] [int] NULL,
    [web_flag] [bit] NULL,
    [partner_id] [varchar](25) NULL,
    [unique_id] [uniqueidentifier] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
4

3 回答 3

1

您可以使用CTE.

  • 选择起始记录
  • 在递归部分获取这些起始记录的子项

SQL 语句

;WITH q AS (
  SELECT  CatID, Parent, Name
  FROM    YourTable
  WHERE   CatID = 3
  UNION ALL
  SELECT  t.CatID, t.Parent, t.Name
  FROM    q
          INNER JOIN YourTable t ON t.Parent = q.CatID
)
SELECT  *
FROM    q
于 2012-04-26T08:58:10.230 回答
1

这样的事情会起作用吗?

DECLARE @CatNode hierarchyid;
SET @CatNode = (
    SELECT CatNode from mz_category
    WHERE CatID = @CatID
  );
SELECT * FROM Items
WHERE CatID IN (
  SELECT  CatID
  FROM    mz_category
  WHERE   CatNode.IsDescendantOf(@CatNode) = 1
);
于 2012-04-26T18:28:17.927 回答
0
DECLARE @CatID hierarchyid = '/6/';

WITH RootCategory AS (
    SELECT  CatID,
            Parent,
            Name
    FROM    Category
    WHERE   CatID = @CatID

    UNION ALL

    SELECT      C.CatID,
                C.Parent,
                C.Name
    FROM        Category C
    INNER JOIN  RootCategory R ON C.Parent = R.CatID
)
SELECT  *
FROM    RootCategory
于 2012-04-26T09:00:49.563 回答