2011-01-23 9 views
2

私はこのようになりますcategoriesテーブルを持っている:これらの2つのテーブルを一緒に結合するには(MySQL、階層型クエリ)?

id | name  | parent  
-----------------------  
1 | Toys  | 1 
2 | Clothing | 1 
3 | Kid's Toys | 0 

私は別のテーブルを持っているが、このようになりますどのcategory_relationships呼ば:

カテゴリー:

id | category_id | parent_id  
----------------------------  
1 | 3   | 1 

は、私は次のような出力を持つようにしたいです:

Toys 
    - Kid's Toys 
Clothing 

これを1つのクエリでどのように達成するには?

+2

階層的なクエリ - 嫌な...厳しいものになる。 DBMSが 'WITH RECURSIVE'節(または 'CONNECT BY'のような非標準拡張)をサポートしていない限り、階層構造の最大深さを知っていなければ、一般的なソリューションをコーディングすることはできません。 –

+0

はい、ツリーの最大の深さを知っていますか? – Xailor

答えて

0

より良い/適切/堅牢な答えはおそらく、このためのMySQLプロシージャを作成なりますが、あなたのデータは、これらの制限に収まることができれば、あなたは以下を使用することができます。

  • 5以下のレベル(または必要に応じて)パターンを拡張
  • IDはせいぜい6桁(または、連結式を変更)

ませんこのクエリでは、などの後に来る名前の子供ようにソート可能なリファレンスを構築するために、連結方式を使用しています連結および先行スペースを使用して手動でインデントされます。

select concat(1000000 + a.id, '|') SORT 
      ,a.name 
    from categories a 
    where a.parent = 1 # top level parents only 
union all 
    select concat(1000000 + a.id, '|', 
      1000000 + IFNULL(b.id,0), '|') 
      ,concat(' - ', b.name) 
    from categories a 
    inner join category_relationships a1 on a1.parent_id = a.id 
    inner join categories b on b.id = a1.category_id 
    where a.parent = 1 
union all 
    select concat(1000000 + a.id, '|', 
      1000000 + IFNULL(b.id,0), '|', 
      1000000 + IFNULL(c.id,0), '|') 
      ,concat(' - ', c.name) 
    from categories a 
    inner join category_relationships a1 on a1.parent_id = a.id 
    inner join categories b on b.id = a1.category_id 
    inner join category_relationships b1 on b1.parent_id = b.id 
    inner join categories c on c.id = b1.category_id 
    where a.parent = 1 
union all 
    select concat(1000000 + a.id, '|', 
      1000000 + IFNULL(b.id,0), '|', 
      1000000 + IFNULL(c.id,0), '|', 
      1000000 + IFNULL(d.id,0), '|') 
      ,concat('  - ', d.name) 
    from categories a 
    inner join category_relationships a1 on a1.parent_id = a.id 
    inner join categories b on b.id = a1.category_id 
    inner join category_relationships b1 on b1.parent_id = b.id 
    inner join categories c on c.id = b1.category_id 
    inner join category_relationships c1 on c1.parent_id = c.id 
    inner join categories d on d.id = c1.category_id 
    where a.parent = 1 
union all 
    select concat(1000000 + a.id, '|', 
      1000000 + IFNULL(b.id,0), '|', 
      1000000 + IFNULL(c.id,0), '|', 
      1000000 + IFNULL(d.id,0), '|', 
      1000000 + IFNULL(e.id,0)) 
      ,concat('  - ', e.name) 
    from categories a 
    inner join category_relationships a1 on a1.parent_id = a.id 
    inner join categories b on b.id = a1.category_id 
    inner join category_relationships b1 on b1.parent_id = b.id 
    inner join categories c on c.id = b1.category_id 
    inner join category_relationships c1 on c1.parent_id = c.id 
    inner join categories d on d.id = c1.category_id 
    inner join category_relationships d1 on d1.parent_id = d.id 
    inner join categories e on e.id = d1.category_id 
    order by SORT 
関連する問題