2015-04-08 68 views
-1

我想用父母,子女和孫子創建多個類別。 並且可以通過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 
    ) 
) 
+4

沒有女人的鞋子不會好。 –

+0

你在使用什麼數據庫系統? –

+0

@AlexK。我用mysql –

回答

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) 
    ) 
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或類似的語言。

0

假設你的表被稱爲「項目」,你可以做以下的遞歸查詢根據級別來產生由所產生的名稱包含父和排序屬性,然後按名稱順序和名稱前添加空格:

;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