0

是否可以像这样按层次顺序对查询表进行排序:

预期的

+----+--------+-----------+-------+--------+-----------+-----------+---------+
| ID | Code   |  Name     | Qty   | Amount | is_parent | parent_id | remarks |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 1  | ABC    | Parent1   |  2    | 1,000  |     1     |     0     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 4  | FFLK   | Product Z |  10   | 2,500  |     0     |     1     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 5  | P6DT   | Product 5 |  7    | 1,700  |     0     |     1     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 6  | P2GL   | Product T |  5    | 1,100  |     0     |     1     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 2  | DHG    | Parent2   |  5    | 1,500  |     1     |     0     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 3  | LMSJ   | Product U |  4    | 600    |     0     |     2     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+

这是原始数据表:

+----+--------+-----------+-------+--------+-----------+-----------+---------+
| ID | Code   |  Name     | Qty   | Amount | is_parent | parent_id | remarks |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 1  | ABC    | Parent1   |  2    | 1,000  |     1     |     0     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 2  | DHG    | Parent2   |  5    | 1,500  |     1     |     0     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 3  | LMSJ   | Product U |  4    | 600    |     0     |     2     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 4  | FFLK   | Product Z |  10   | 2,500  |     0     |     1     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 5  | P6DT   | Product 5 |  7    | 1,700  |     0     |     1     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+
| 6  | P2GL   | Product T |  5    | 1,100  |     0     |     1     | xxx     |
+----+--------+-----------+-------+--------+-----------+-----------+---------+


is_parent column = 1 if data row set to parent, 0 if data row set to child 
parent_id column = 0 if data row set to parent, depend on ID of parent data

我正在使用 SQL Server 生成数据。

4

1 回答 1

0

看起来实际的问题是如何按层次顺序查询数据。这可以使用递归查询来实现,但更快的替代方法是使用 SQL Server 对分层数据的支持。

以分层顺序返回数据的递归查询如下所示:

WITH h AS
(
      SELECT
             ID,Code,Name,Qty,Amount,is_parent,parent_id,remarks
      FROM
            dbo.ThatTable
      WHERE 
            parent_id=0
      UNION ALL 
      SELECT
             c.ID,c.Code,c.Name,c.Qty,c.Amount,c.is_parent,c.parent_id,c.remarks
      FROM
            dbo.ThatTable c
      INNER JOIN h ON 
            c.parent_id= h.Id
)
SELECT * FROM h

ID如果和Parent_ID字段被索引,这个查询的性能是可以接受的,但不是很好。

向表中添加hierarchyid字段将使查询更简单、更快。假设有一个hierarchy字段,查询将是:

SELECT *
FROM ThatTable
ORDER BY hierarchy

hierarchy在此查询和任何查找特定节点的子节点的查询上添加索引,速度非常快。服务器无需递归查询,只需查看该单个索引即可。

文章第 1 课:将表转换为层次结构展示了如何使用 a 创建一个新表hierarchyid并从父/子数据填充它。

于 2019-10-04T09:17:48.680 回答