2017-08-10 9 views
-1

私はこのようなテーブルを持っています。 parent_tree列は、コロン区切りの親ツリーが含まれているすなわち グランドグランド親グランド親親子の注文 - SQLテーブル内の階層データ

parent_tree        | name    | Type 

All Liabilities:Current Liabilities  | Salaries Payable | Liabilities 
Assets         | Current Assets  | Assets 
null         | All Liabilities  | Parent 
All Liabilities       | Current Liabilities | Parent 
null         | Assets    | Parent 
. 
. 

質問 は、今私は(---を選択することにより、テーブルの順序から選択クエリを実行したい ----)を使用して、最初に親とそれに続く子の順番で結果を取得します。

parent_tree        | name    | Type 

null         | All Liabilities  | Parent 
All Liabilities       | Current Liabilities | Parent 
All Liabilities:Current Liabilities  | Salaries Payable | Liabilities 
null         | Assets    | Parent 
Assets         | Current Assets  | Assets 

したがって、主な質問は何を書き込むべきかです

によってために、eは私は次の試みたが、結果が正しい順序ではありません。

select * from table order by if(type = 'Parent', concat(coalesce(parent_tree,name), ':' ,name), parent_tree), length(parent_tree), if(type = 'Parent', 0, 1), type"; 

答えて

0

私は次のクエリ

select * from table order by 
order by concat( if(parent_tree is null, name, concat(parent_tree, ':', name)), ':'); 
でこれを解決しました
関連する問題