简体   繁体   English

对数据库中的双精度值进行排序

[英]Sorting of double values from database

In the database I keep records to which codes are assigned (double format). 在数据库中,我保留分配代码的记录(双格式)。 When calling records from the database I would like them to be sorted from the smallest to the largest one. 从数据库调用记录时,我希望它们从最小到最大排序。

The problem arises when 1.2 > 1.10 which is understandable. 当1.2> 1.10可以理解时,就会出现问题。 I would like it to be interpreted as follows: 1.10 > 1.2 我希望将其解释如下:1.10> 1.2

I tried to do this by changing the type to TEXT. 我试图通过将类型更改为TEXT来做到这一点。 Adding spaces at the end of the "code". 在“代码”的末尾添加空格。

I have an idea to divide the numbers into segments and if the first segment is equal then sort by the second segment. 我有一个主意,将数字分成几个部分,如果第一个部分相等,则按第二个部分排序。 It seems logical, but I don't know how to translate it into code. 看来合乎逻辑,但我不知道如何将其转换为代码。

Could anyone help? 有人可以帮忙吗?

if your codes contain a valid decimal value the you could try converting the codes in order by as couple of integer 如果您的代码包含有效的十进制值,则可以尝试按整数对顺序转换代码

select codes 
from my_table
order by  ( CAST(left(codes, locate('.', codes)-1 ) AS UNSIGNED), 
    CAST(right(codes, length(codes -locate('.', codes)) AS UNSIGNED))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM