简体   繁体   English

SQL ORDER BY难题与数字

[英]SQL ORDER BY dilemma with numbers

I have a table which holds a varchar datatype. 我有一个表,其中包含varchar数据类型。 It holds 128 characters max. 最多可容纳128个字符。

I'm trying to order it alphabetically, and it works out fine, except for one little thing. 我正在尝试按字母顺序对其进行排序,但除了一件小事之外,它都可以正常工作。

When I try to save a mixture of numbers and letters, it returns the 'literal' alphabetical order, which means 11 comes first before 2. 当我尝试保存数字和字母的混合形式时,它将返回“文字”字母顺序,这意味着11排在2之前。

I have read almost all of the answers in the internet, but they are all workarounds that cannot work specifically for my problem. 我已经阅读了互联网上几乎所有的答案,但是它们都是无法专门解决我的问题的解决方法。

Examples of values I want to put in order 我想排序的值的示例

Apartment
House
Dog
Cat
18 years old
2 years old
1 year old

But I want it to look like this. 但我希望它看起来像这样。

1 year old
2 years old
18 years old
Apartment
Cat
Dog
House

It spans on a large database and I can't just split the numerical values apart from the text ones. 它跨越一个大型数据库,我不能只将数字值与文本值分开。

Also users who can use the program can modify it with Alphanumeric characters. 也可以使用该程序的用户可以用字母数字字符对其进行修改。

Any suggestions about my problem? 关于我的问题有什么建议吗? Thanks. 谢谢。

Here is something I tried in SQL Server. 这是我在SQL Server中尝试过的东西。 It's neither elegant nor fit for production, but it may give you an idea. 它既不优雅也不适合生产,但可能会给您一个想法。

SELECT StringValue, 
    CAST(SUBSTRING(StringValue, StartPos, EndPos - StartPos) AS INT) AsNumber,
    SUBSTRING(StringValue, StartPos, EndPos - StartPos) NumberToken,
    SUBSTRING(StringValue, EndPos, 1000) Rest,
    StartPos, 
    EndPos
FROM    
    (SELECT 
        StringValue,
        PATINDEX('[0-9]%', StringValue) StartPos,
        PATINDEX('%[^0-9]%', StringValue) EndPos
    FROM 
        (SELECT 'abc123xyz' StringValue
        UNION SELECT '1abc'
        UNION SELECT '11abc'
        UNION SELECT '2abc'
        UNION SELECT '100 zasdfasd') Sub1
    ) Sub2
ORDER BY AsNumber, Rest

Result: 结果:

StringValue       AsNumber        NumberToken  Rest            StartPos     EndPos
abc123xyz                 0                    abc123xyz               0          1
1abc                      1                  1 abc                     1          2
2abc                      2                  2 abc                     1          2
11abc                    11                 11 abc                     1          3
100 zasdfasd            100                100  zasdfasd               1          4

I would approach this as follows... 我将按照以下方式进行处理...

First, write an expression to convert the numeric stuff to integers, something like 首先,编写一个表达式将数字内容转换为整数,例如

select CAST(SUBSTRING(<field>',1,instr(<field>',' ') as INT),<field>

I would then use a UNION ALL statement, something like this 然后,我将使用UNION ALL语句,如下所示

SELECT CAST(SUBSTRING(<field>',1,instr(<field>',' ') as INT),<field>,A.*
FROM <table> A
WHERE <field> LIKE <regular expression to get fields beginning with numbers>
UNION ALL
SELECT 999999,<field>,A.*
FROM <table> A
WHERE <field> NOT LIKE <regular expression to get fields beginning with numbers>
ORDER BY 1,2,3

The numbers will appear first, in numeric order. 这些数字将首先以数字顺序出现。 Sine all of the alpha data has the same numeric key, it will appear sorted alphabetically after the numbers... Just be sure to make the alpha dummy key (999999) is large enough to be after all the numeric ones... 正弦所有的alpha数据都具有相同的数字键,它将按字母顺序显示在数字之后...只需确保将alpha虚拟键(999999)设置得足够大,可以跟在所有数字键之后...

I don't have mySQL on this machine, but hopefully this gives you enough of a start to solve it 我在这台机器上没有mySQL,但是希望这可以给您足够的开始解决它的方法

您应该通过执行以下操作来摆脱困境:

order by right(replicate(' ',30)+Column_name,30)

Try this order by: 尝试以下命令:

ORDER BY RIGHT(REPLICATE('0',128)+value,128)

My test: 我的测试:

DECLARE @T TABLE
(
value VARCHAR(128)
)

INSERT INTO @T VALUES('Apartment'),
('House'),
('Dog'),
('Cat'),
('18 years old'),
('2 years old'),
('1 year old'),
('12 horses'),
('1 horse')


SELECT * FROM @T
ORDER BY RIGHT(REPLICATE('0',128)+value,128)

RESULTS: 结果:

Cat
Dog
House
1 horse
12 horses
Apartment
1 year old
2 years old
18 years old

If you find a case that this doesn't work please post it along with the sort order you would like and I can see if there's a fix. 如果您发现这种情况不起作用,请将其与您想要的排序顺序一起发布,我可以看看是否有解决方法。

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

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