我有一个问题understandint ORDER BY在MySQL.我必须按3个标准对表格进行排序
1 - 首先我想按TYPE_OF_WORK排序,所以所有数据必须是按字母顺序排列的,比如
dech_rap_bus
dech_rap_bus
ger_dem_dech_bus
ger_dem_dech_bus
ger_dem_stp_pp
...
Run Code Online (Sandbox Code Playgroud)
结果=> http://sqlfiddle.com/#!2/b2a858/6
2秒我希望按PROJECT_VERSION排序,所以所有数据必须是按字母顺序排列的,但要尊重1个标准,例如
dech_rap_bus V123-V1234
dech_rap_bus V300
ger_dem_dech_bus V123-V1234
ger_dem_dech_bus V300
ger_dem_stp_pp V123-V1234
Run Code Online (Sandbox Code Playgroud)
结果=> http://sqlfiddle.com/#!2/b2a858/7
所以1和2工作得很好.
3 - 之后我想按列not_existing排序
结果=> http://sqlfiddle.com/#!2/b2a858/5
我不知道它到底做了什么,但我看不到任何结果......我只想要那个
dech_rap_bus V300
其中NOT_EXISTING列是1是在端部,并且是多个时NOT_EXISTING = 1到它们全部进行排序,但在表的末尾.
我认为2个选择的联盟会帮助我
/* Selecting all data where not_existing is not 1 or NULL ---> working good! */
(
SELECT
*
FROM
atm_mti_view
WHERE
project_function='FRS01' AND
on_big_project_id = 12 AND
(not_existing != 1 OR not_existing IS NULL)
ORDER BY
type_of_work ASC,
project_version ASC
)
UNION
/* Selecting all data where not_existing is 1 ---> working good! */
(
SELECT
*
FROM
atm_mti_view
WHERE
project_function='FRS01' AND
on_big_project_id = 12 AND
not_existing = 1
ORDER BY
type_of_work ASC,
project_version ASC
)
Run Code Online (Sandbox Code Playgroud)
但是这段代码做了什么,就是把不存在的dech_rap_bus放到最后,好,但是它弄乱了版本排序,为什么?
在这里看到结果=> http://sqlfiddle.com/#!2/b2a858/8
这是为什么?我只想合并两个选择结果,我做错了什么?
谢谢.
如果你这样做
order by
(case when not_existing is null then 0 else not_existing end) desc
,type_of_work ASC,
project_version ASC
Run Code Online (Sandbox Code Playgroud)
它将首先出现。
您的查询未排序,因为您的 dech_rap_bus 具有不同的项目值
TYPE_OF_WORK PROJECT_VERSION
dech_rap_bus V123-V1234
dech_rap_bus V300
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
143 次 |
| 最近记录: |