MySQL'Order By'-正确排序字母数字
我想按下面显示的顺序对它们进行排序(数字1-12):
1个2
3
4
5
6
7
8
9
10
11
12
但是,我的查询-使用order by xxxxx asc按其他所有字母开头的数字排序:
1个10
11
12
2
3
4
5
6
7
8
9
有什么技巧可以使其更正确地排序吗?
此外,为了充分公开,这可以是字母和数字的混合(尽管现在不是),例如:
A1534克
G46A
100B
100A
100JE
等等....
谢谢!
select * from table order by name asc回答:
人们使用不同的技巧来做到这一点。我搜索了一下,发现每个结果都遵循不同的技巧。看看他们:
- MySQL中的字母数字排序
- MySQL中的自然排序
- 数字值与字母数字值混合排序
- MySQL自然排序
- MySQL中的自然排序
编辑:
我刚刚为以后的访问者添加了每个链接的代码。
MySQL中的字母数字排序
1A 1a 10A 9B 21C 1C 1D1A 1C 1D 1a 9B 10A 21CBin Way===================================
SELECT 
tbl_column, 
BIN(tbl_column) AS binray_not_needed_column
FROM db_table
ORDER BY binray_not_needed_column ASC , tbl_column ASC
-----------------------
Cast Way
===================================
SELECT 
tbl_column, 
CAST(tbl_column as SIGNED) AS casted_column
FROM db_table
ORDER BY casted_column ASC , tbl_column ASC
MySQL中的自然排序
表:sorting_test -------------------------- -------------
| 字母数字VARCHAR(75)| 整数INT |
 -------------------------- -------------
| test1 | 1 |
| test12 | 2 |
| test13 | 3 |
| test2 | 4 |
| test3 | 5 |
 -------------------------- -------------
 -------------------------- -------------| alphanumeric VARCHAR(75) | integer INT |
 -------------------------- -------------
| test1                    | 1           |
| test2                    | 4           |
| test3                    | 5           |
| test12                   | 2           |
| test13                   | 3           |
 -------------------------- -------------
SELECT alphanumeric, integer       FROM sorting_test
       ORDER BY LENGTH(alphanumeric), alphanumeric
数字值与字母数字值混合排序
2a, 12, 5b, 5a, 10, 11, 1, 4b1, 2a, 4b, 5a, 5b, 10, 11, 12SELECT versionFROM version_sorting
ORDER BY CAST(version AS UNSIGNED), version;
希望这可以帮助
以上是 MySQL'Order By'-正确排序字母数字 的全部内容, 来源链接: utcz.com/qa/433584.html







