使用父,子和孙创建多个类别

时间:2015-04-08 12:58:13

标签: mysql sql

我想用父,子和孙创建多个类别。 并且可以按ordering字段排序。

id  | parent_id |   name    |  ordering
--------------------------------------
1   |     0     |   Men     |   1
2   |     0     |   Women   |   2
3   |     1     |   Shoes   |   3
4   |     2     |   Watches |   4
5   |     1     |   Pants   |   5
6   |     3     |   Sport   |   6
7   |     3     |   Casual  |   7
8   |     2     |   Book    |   8

我想要这个结果

Men
  Shoes
     Sport
     Casual
  Pants
Women
  Watches
  Book

数组输出

Array
(
[0] => Array
    (
        [id] => 1
        [parent_id] => 0
        [name] => Men
        [ordering] => 1
        [level] => parent    //NEEDED
    )
[1] => Array
    (
        [id] => 2
        [parent_id] => 0
        [name] => Women
        [ordering] => 2
        [level] => parent    //NEEDED
    )
[2] => Array
    (
        [id] => 3
        [parent_id] => 1
        [name] => Shoes
        [ordering] => 3
        [level] => child    //NEEDED
    )
[3] => Array
    (
        [id] => 4
        [parent_id] => 2
        [name] => Watches
        [ordering] => 4
        [level] => child    //NEEDED
    )
[4] => Array
    (
        [id] => 5
        [parent_id] => 1
        [name] => Pants
        [ordering] => 5
        [level] => child    //NEEDED
    )
[5] => Array
    (
        [id] => 6
        [parent_id] => 3
        [name] => Sport
        [ordering] => 6
        [level] => grandchild    //NEEDED
    )
[6] => Array
    (
        [id] => 7
        [parent_id] => 3
        [name] => Casual
        [ordering] => 7
        [level] => grandchild    //NEEDED
    )
[7] => Array
    (
        [id] => 8
        [parent_id] => 2
        [name] => Book
        [ordering] => 8
        [level] => child    //NEEDED
    )
)

3 个答案:

答案 0 :(得分:0)

这可以是获取您的关卡信息的解决方案:

SELECT CASE WHEN parent_id = 0 THEN 'Parent'
       ELSE CASE WHEN (SELECT T2.parent_id 
                        FROM tab T2
                        WHERE T2.id = T.parent_id) = 0 THEN 'Child'
                  ELSE 'Grandchild' 
             END
   END AS level
FROM tab T

Fiddle here

要将此SQL的内容放入数组中,您需要一种像PHP这样的服务器端语言,因为您不使用mongoDB或类似的。

答案 1 :(得分:0)

假设您的表被调用" items",您可以执行以下递归查询以生成包含父级和排序属性的名称,然后按生成的名称排序并根据名称在名称前添加空格电平:

;WITH itemsHierarchy (id, parent_id, name, generatedname, level)
AS
(
    SELECT 
        i.id, 
        i.parent_id, 
        i.name, 
        CAST(i.name AS varchar(MAX)) as generatedname, 
        1 as level
    FROM items i
    WHERE i.parent_id = 0
    UNION ALL
    SELECT 
        i.id, 
        i.parent_id, 
        i.name,
         CAST(ih.generatedname + '_' + CAST(i.ordering as varchar(2))+ '_' + i.name AS varchar(MAX)), 
         ih.level + 1
    FROM items i
    INNER JOIN itemsHierarchy ih ON i.parent_id = ih.id
)

SELECT REPLICATE('   ', level) + name
FROM itemsHierarchy
ORDER BY generatedname

结果:

   Men
      Shoes
         Sport
         Casual
      Pants
   Women
      Watches
      Book

答案 2 :(得分:0)

最后我找到了很好的答案

参考:Parent child mysql

select t0.*,
  concat(
      case coalesce(t4.parent_id, 0) 
        when 0 then ''
        else concat(cast(t4.parent_id as char), '\\')
      end,
      case coalesce(t3.parent_id, 0) 
        when 0 then ''
        else concat(cast(t3.parent_id as char), '\\')
      end,
      case coalesce(t2.parent_id, 0) 
        when 0 then ''
        else concat(cast(t2.parent_id as char), '\\')
      end,
      case coalesce(t1.parent_id, 0) 
        when 0 then ''
        else concat(cast(t1.parent_id as char), '\\')
      end,
      case coalesce(t0.parent_id, 0) 
        when 0 then ''
        else concat(cast(t0.parent_id as char), '\\')
      end,
      cast(t0.id as char)
    ) as path
from mytable t0
    left join mytable t1 on t0.parent_id = t1.Id
    left join mytable t2 on t1.parent_id = t2.Id
    left join mytable t3 on t2.parent_id = t3.Id
    left join mytable t4 on t3.parent_id = t4.Id
order by 
  concat(
      case coalesce(t4.parent_id, 0) 
        when 0 then ''
        else concat(cast(t4.parent_id as char), '\\')
      end,
      case coalesce(t3.parent_id, 0) 
        when 0 then ''
        else concat(cast(t3.parent_id as char), '\\')
      end,
      case coalesce(t2.parent_id, 0) 
        when 0 then ''
        else concat(cast(t2.parent_id as char), '\\')
      end,
      case coalesce(t1.parent_id, 0) 
        when 0 then ''
        else concat(cast(t1.parent_id as char), '\\')
      end,
      case coalesce(t0.parent_id, 0) 
        when 0 then ''
        else concat(cast(t0.parent_id as char), '\\')
      end,
      cast(t0.id as char)
    )