MySql内连接顺序

时间:2013-08-09 10:03:00

标签: mysql sql

我正在使用菜单模块,并在检索菜单时遇到问题。

Kindly check the fiddle for reference.

这里在数据库中:PARENTID表示它是父母与否。 如果PARENTID = 0,则为父菜单,否则为子菜单。

PARENTLEVEL - 等级为1,2,3,4,5,6 ..

当我运行查询时,我能够按顺序获取菜单:

  

SELECT * FROM category WHERE publish ='1'和parentid ='0'顺序menuorder ASC

但是当我想订购Sub Menus时,我无法订购它们:

  

SELECT * FROM c类内部联接类别b在c.parentid = b.parentid,其中c.publish ='1'和c.parentid!='0'

我试过了:

  

SELECT * FROM category p LEFT JOIN category c ON   c.parentid = p.parentid WHERE p.publish = 1 order by   p.PARENTID,c.MENUORDER;

我正在尝试的是:

我希望结果按照这样的顺序:

    1.Home
    2.Settings
          Set Period
          User Type
          Roles
          Region
   .....etc

所以,我可以获得菜单顺序。有人可以帮我订购主菜单下的菜单/子菜单吗?

提前致谢!!

1 个答案:

答案 0 :(得分:0)

好的,这里是edited version

SELECT p.menuname pmen,c.menuname cmen,
 COALESCE(c.menutype,p.menutype) menutype,
 COALESCE(c.link,p.link) link,
 COALESCE(c.accessgroup,p.accessgroup) accessgroup
FROM category p 
LEFT JOIN category c ON  c.parentid=p.id AND c.publish=1 
WHERE p.publish=1 and p.parentid='0'  
ORDER BY p.menuorder,c.menuorder

结果:

|            PMEN |                    CMEN | MENUTYPE |     LINK | ACCESSGROUP |
---------------------------------------------------------------------------------
|            Home |                  (null) | leftmenu | home.php |           1 |
|        Settings |              Set Period | leftmenu | home.php |           1 |
|        Settings |               User Type | leftmenu | home.php |           1 |
|        Settings |                   Roles | leftmenu | home.php |           1 |
|        Settings |                  Region | leftmenu | home.php |           1 |
|        Settings |                District | leftmenu | home.php |           1 |
|        Settings |                   State | leftmenu | home.php |           1 |
|        Settings |                    City | leftmenu | home.php |           1 |
|  Master Screens |            Product Type | leftmenu | home.php |           1 |
|  Master Screens |      Beneficiary Master | leftmenu | home.php |           1 |
|  Master Screens |        Component Master | leftmenu | home.php |           1 |
|  Master Screens |           Sub-Component | leftmenu | home.php |           1 |
|  Master Screens |              LOT Master | leftmenu | home.php |           1 |
|  Master Screens |           Tender Master | leftmenu | home.php |           1 |
|  Master Screens |         PDI Task Master | leftmenu | home.php |           1 |
| User Management |                    User | leftmenu | home.php |           1 |
| User Management |                    RBAC | leftmenu | home.php |           1 |
| TPI Task Master |                  (null) | leftmenu | home.php |           1 |
|    Transaction  |              Work Order | leftmenu | home.php |           1 |
|    Transaction  |                  Vendor | leftmenu | home.php |           1 |
|    Transaction  |          PDI Inspection | leftmenu | home.php |           1 |
|    Transaction  |          TPI Inspection | leftmenu | home.php |           1 |
|    Transaction  |           DE Inspection | leftmenu | home.php |           1 |
|    Transaction  |           RM Inspection | leftmenu | home.php |           1 |
|    Transaction  | Install & Commissioning | leftmenu | home.php |           1 |
|    Transaction  |         Re-Installation | leftmenu | home.php |           1 |
|    Transaction  |          Bank Guarantee | leftmenu | home.php |           1 |
|    Transaction  |        Initiate Invoice | leftmenu | home.php |           1 |
|        Accounts |        Invoice Approval | leftmenu | home.php |           1 |
|         Reports |                  (null) | leftmenu | home.php |           1 |