This question already has an answer here:
这个问题在这里已有答案:
- Natural Sort in MySQL 19 answers
MySQL中的自然排序19答案
Baiscally, my results are shown like this with ORDER BY titel ASC
在Baiscally,我的结果显示为ORDER BY titel ASC
- 'test 1'
- 'test 10'
- 'test 2'
- 'test 20'
- 'test 3'
- 'test 30'
However, I would like to have them structured like this:
但是,我希望它们的结构如下:
- 'test 1'
- 'test 2'
- 'test 3'
- 'test 10'
- 'test 20'
- 'test 30'
Do you have any ideas on how I could fix this? I don't know, but it seems like my brain isn't working right now.
你对我如何解决这个问题有什么想法吗?我不知道,但似乎我的大脑现在不能正常工作。
2 个解决方案
#1
2
You can sort by length as well (assuming the prefix stays the same); it would work on your given test case:
您也可以按长度排序(假设前缀保持不变);它适用于您给定的测试用例:
ORDER BY LENGTH(titel), titel ASC;
#2
-2
sort by field name, what's big deal in it? below is sql and php code for this either one you may need:
按字段名称排序,有什么大不了的?下面是你可能需要的sql和php代码:
SELECT *
FROM `posts`
ORDER BY `post_title` ASC
LIMIT 0 , 30
php below
<?php $sql = 'SELECT *FROM `posts` ORDER BY `post_title` ASC LIMIT 0, 30 '; ?>
I'm sorry I wasn't able to understand the question, below is the possible soultion in comments, thanks
对不起,我无法理解这个问题,下面是评论中可能的灵魂,谢谢
#1
2
You can sort by length as well (assuming the prefix stays the same); it would work on your given test case:
您也可以按长度排序(假设前缀保持不变);它适用于您给定的测试用例:
ORDER BY LENGTH(titel), titel ASC;
#2
-2
sort by field name, what's big deal in it? below is sql and php code for this either one you may need:
按字段名称排序,有什么大不了的?下面是你可能需要的sql和php代码:
SELECT *
FROM `posts`
ORDER BY `post_title` ASC
LIMIT 0 , 30
php below
<?php $sql = 'SELECT *FROM `posts` ORDER BY `post_title` ASC LIMIT 0, 30 '; ?>
I'm sorry I wasn't able to understand the question, below is the possible soultion in comments, thanks
对不起,我无法理解这个问题,下面是评论中可能的灵魂,谢谢