mysql没有正确排序

时间:2013-04-10 13:46:08

标签: mysql sql-order-by

我的下表有条目

+--------------------------------------------+----------+
| menu_entry                                 | position |
+--------------------------------------------+----------+
| ADMINISTRATION::USERMANAGEMENT::USER       |        2 |
| DEMO::CHART::EXAMPLE                       |        1 |
| PROJMGMT::PROJSTRG::LOP                    |        2 |
| ADMINISTRATION::USERMANAGEMENT::RIGHTSMMGT |        1 |
| PROJMGMT::PROJSTRG::MEETINGS               |        1 |
| DEMO::GRID::CELLEDIT                       |        1 |
| DEMO::GRID::ROWEDIT                        |        3 |
| DEMO::DATAMGMT::GROUPING                   |        1 |
| DEVELOPER::APPS::ADDAPP                    |        2 |
| DEVELOPER::APPS::APPTEST                   |        1 |
| DEMO::GRID::LOCKINGDEMO                    |        4 |
| DEMO::GRID::FILTERDEMO                     |        2 |
+--------------------------------------------+----------+

我希望按menu_entry排名第一和第二。我使用以下Statement

执行此操作
 select menu_entry,position from app_names order by menu_entry, position;

但它只按menu_entry排序,而不是按位置排序。 这是我的陈述的回报:

+--------------------------------------------+----------+
| menu_entry                                 | position |
+--------------------------------------------+----------+
| ADMINISTRATION::USERMANAGEMENT::RIGHTSMMGT |        1 |
| ADMINISTRATION::USERMANAGEMENT::USER       |        2 |
| DEMO::CHART::EXAMPLE                       |        1 |
| DEMO::DATAMGMT::GROUPING                   |        1 |
| DEMO::GRID::CELLEDIT                       |        1 |
| DEMO::GRID::FILTERDEMO                     |        2 |
| DEMO::GRID::LOCKINGDEMO                    |        4 |
| DEMO::GRID::ROWEDIT                        |        3 |
| DEVELOPER::APPS::ADDAPP                    |        2 |
| DEVELOPER::APPS::APPTEST                   |        1 |
| PROJMGMT::PROJSTRG::LOP                    |        2 |
| PROJMGMT::PROJSTRG::MEETINGS               |        1 |
+--------------------------------------------+----------+

如您所见,DEMO :: GRID :: ...未正确排序。

1 个答案:

答案 0 :(得分:4)

您应该按SUBSTRING_INDEX(menu_entry, '::', 1)排序,提取第一个::SUBSTRING_INDEX(menu_entry, '::', 2)左侧的字符串部分,具体取决于您所使用的内容:

select menu_entry, position
from app_names
order by
  SUBSTRING_INDEX(menu_entry, '::', 1), position;

请参阅小提琴here