MySQL排序依据(其中menu_name = child_name)

时间:2018-09-24 15:46:20

标签: mysql

我有一些mysql查询来显示菜单。我要菜单由父母和孩子订购。这是我的基本查询:

select * from (
select a.menu_id, a.menu_name, a.menu_parent parent, a.menu_parent parent_id, a.menu_order 
from menu_backend a
where a.menu_parent = '0'

union all

select b.menu_id, concat('--',b.menu_name) menu_name, c.menu_name parent, b.menu_parent parent_id, b.menu_order
from menu_backend b
left join menu_backend c on c.menu_id = b.menu_parent
where b.menu_parent != '0'
) alias

这就是结果:

http://xomf.com/vgjgc

我想完全按照父级顺序订购子菜单,但是我不知道该怎么做。 谢谢

1 个答案:

答案 0 :(得分:0)

我没有对您的基本SQL进行很多修改,如何处理?

select *
  from (select a.menu_id,
               a.menu_name,
               a.menu_parent parent,
               a.menu_parent parent_id,
               a.menu_order,
               a.menu_id     sort_id
          from menu_backend a
         where a.menu_parent = '0'
        union all
        select b.menu_id,
               concat('--', b.menu_name) menu_name,
               c.menu_name parent,
               b.menu_parent parent_id,
               b.menu_order,
               b.menu_parent sort_id
          from menu_backend b
          left join menu_backend c
            on c.menu_id = b.menu_parent
         where b.menu_parent != '0') alias
 order by sort_id, parent_id, menu_order