在mysql中自定义顺序

时间:2015-09-19 13:13:58

标签: mysql sql-order-by

我有一张表,其中有3个字段

+------+------+------+
| name | type | rank |
+------+------+------+
| abc  | A    | 1    |
| def  | B    | 1    |
| ghi  | A    | 2    |
| jkl  | C    | null |
| mno  | B    | 2    |
| pqr  | C    | 1    |
+------+------+------+

如何首先按类型(A然后B然后是C)对该表进行排序,然后按等级按升序排序?这样,在排名>之后排名为null 0?

1 个答案:

答案 0 :(得分:1)

您可以在09-19 14:53:00.376: E/MainActivity(28935): Error in creating fragment 09-19 14:53:00.387: D/AndroidRuntime(28935): Shutting down VM 09-19 14:53:00.394: E/AndroidRuntime(28935): FATAL EXCEPTION: main 09-19 14:53:00.394: E/AndroidRuntime(28935): Process: info.androidhive.slidingmenu, PID: 28935 09-19 14:53:00.394: E/AndroidRuntime(28935): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.app.ActivityThread.access$800(ActivityThread.java:148) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.os.Handler.dispatchMessage(Handler.java:102) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.os.Looper.loop(Looper.java:135) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.app.ActivityThread.main(ActivityThread.java:5312) 09-19 14:53:00.394: E/AndroidRuntime(28935): at java.lang.reflect.Method.invoke(Native Method) 09-19 14:53:00.394: E/AndroidRuntime(28935): at java.lang.reflect.Method.invoke(Method.java:372) 09-19 14:53:00.394: E/AndroidRuntime(28935): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 09-19 14:53:00.394: E/AndroidRuntime(28935): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 09-19 14:53:00.394: E/AndroidRuntime(28935): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1243) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.app.Activity.performStart(Activity.java:5969) 09-19 14:53:00.394: E/AndroidRuntime(28935): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277) 09-19 14:53:00.394: E/AndroidRuntime(28935): ... 10 more

中使用CASE个表达式
ORDER BY

演示:SQL Fiddle

你也可以使用:

SELECT *
FROM YourTable
ORDER BY type
        ,CASE WHEN rank IS NULL THEN 1 ELSE 0 END
        ,rank

但是你必须选择一个数字,该字段永远不会高于,第一种方法消除了猜测。