我有一张这样的桌子。 parent_tree列包含冒号分隔的父树,即 Grand Grand Parent : Grand Parent : Parent
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
.
.
问题 现在我想运行一个select查询(从----中的表顺序中选择---)以按以下顺序获得结果,即父项首先,然后是子项:
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
所以主要的问题是我应该用顺序
写什么我试过了,但结果的顺序不正确:
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 :(得分:0)
我用以下查询
解决了这个问题select * from table order by
order by concat( if(parent_tree is null, name, concat(parent_tree, ':', name) ), ':');