SELECT
programs.name AS program,
program_categories.name AS category
FROM programs
INNER JOIN program_categories
ON program_category_id = program_categories.id
ORDER BY
program_categories.name,
programs.name
上面的代码应提供以下结果(我认为):
Module 1: | foo
Module 1: | foo
Module 2: | foo
Module 3: | foo
Module 4: | foo
Module 5: | foo
Module 6: | foo
Module 7: | foo
Module 8: | foo
Module 9: | foo
Module 10: | foo
Module 11: | foo
Module 12: | foo
Module 13: | foo
但是却产生:
Module 10: | foo
Module 11: | foo
Module 12: | foo
Module 13: | foo
Module 1: | foo
Module 1: | foo
Module 2: | foo
Module 3: | foo
Module 4: | foo
Module 5: | foo
Module 6: | foo
Module 7: | foo
Module 8: | foo
Module 9: | foo
有人介意解释什么导致了我似乎不了解的现象吗?以及如何解决/解决它?
答案 0 :(得分:2)
一种简单的方法-如果category
中的初始字符串都相同-首先按长度排序,然后按值排序:
ORDER BY LENGTH(category), category, program