0
我需要排序查询的结果由varchar
可能包含数字,但也可以事先排序其他字段。排序varchar数字时也排序其他字段
说我的表如下所示:
+------------------------+-----------------+
| AnotherField | VarCharWithNums |
+------------------------+-----------------|
| Same Data in Every Row | Numbers 5-10 |
| Same Data in Every Row | Numbers 10-13 |
| Same Data in Every Row | Numbers 13-15 |
+------------------------+-----------------|
这个查询:
SELECT VarCharWithNums, AnotherField
FROM MyTable
ORDER BY CAST(VarCharWithNums AS UNSIGNED) ASC
给了我这样的:
+------------------------+-----------------+
| AnotherField | VarCharWithNums |
+------------------------+-----------------|
| Same Data in Every Row | Numbers 5-10 |
| Same Data in Every Row | Numbers 10-13 |
| Same Data in Every Row | Numbers 13-15 |
+------------------------+-----------------|
这个查询:
SELECT VarCharWithNums, AnotherField
FROM MyTable
ORDER BY AnotherField ASC, CAST(VarCharWithNums AS UNSIGNED) ASC
给了我这样的:
+------------------------+-----------------+
| AnotherField | VarCharWithNums |
+------------------------+-----------------|
| Same Data in Every Row | Numbers 10-13 |
| Same Data in Every Row | Numbers 5-10 |
| Same Data in Every Row | Numbers 13-15 |
+------------------------+-----------------|
不要紧,重点是什么,我给的字段ORDER BY
子句中,它从来没有正确排序VarCharWithNums
当我为了它与其他领域。
http://stackoverflow.com/questions/5417381/mysql-sort-string-number –
您可能需要解析出该字段以获取只有一个数字,并使用它进行排序(或两个,取决于你如何想要对范围进行排序) –
如果'AnotherField'的每一行都有相同的数据,为什么要对它进行排序?或者这只是人为的例子的一部分?当我设置测试用例并尝试时,我没有任何问题。 –