2016-05-04 196 views
0

我想通过mysql对字母数字数据进行排序。我的数据是一些典型的类型,如:通过MySql对字母数字数据进行排序

XYZ-1.0-7.0-1 
XYZ-1.0-27.0-5.7 
XYZ-1.0-20.0-4.6 
XYZ-1.0-10.0-2.4 
----------------- many more data in this format ------------ 

我GOOGLE了,发现很多链接,但都没有工作。

我想这个数据作为输出:

XYZ-1.0-7.0-1 
XYZ-1.0-10.0-2.4 
XYZ-1.0-20.0-4.6 
XYZ-1.0-27.0-5.7 
+0

什么是第一位的* 1.11 *或* 1.2 *? –

+0

订单将1.0然后1.1等等。它在所有情况下在“ - ”符号后使用。 – steven

回答

0

从删除.-值和排序。

查询

SELECT * FROM your_table_name 
ORDER BY CAST((
    REPLACE(REPLACE(
     RIGHT(your_column_name, LENGTH(your_column_name) - 4), '.', ''), '-', '')) 
AS UNSIGNED); 

SQL Fiddle Demo

+0

如果一个值为:('XYZ-1.1-7.0-1') – steven

+0

@steven:我已经添加了值“XYZ-1.1-7.0-1”,它的工作正常。请检查这个小提琴。 http://sqlfiddle.com/#!9/8aec82/1 – Wanderer

+0

如果我们添加“XYZ-1.1-7.0-1”,那么它应该在底部。 – steven

0

你想要什么全自动没有你的帮助的计算机无法做到的。要排序这个值,你需要“教”查询,这是你的字符串的格式。如果格式始终与“XXX-9.9-9.9-9.9”相同,则需要创建一个查询,将您的字符串分割为值,然后进行排序。 喜欢的东西:

SELECT * FROM your_table_name ORDER BY SUBSTRING(col,3), substring(col,5,2), substring(col,5,2), substring(col,9,2) 

的另一个问题是,你的数值并不总是相同的字符串格式:比如你在第三部分“7.0”和“27.0”。如果格式化为“07.0”和“27.0”的thoose值可用。要使用thoose值,您需要将它们转换为数字。 希望这有助于。

0

这个查询将您的字符串分成几部分:

select t.col 
    , cast(num1 as unsigned) n1 
    , cast(num2 as unsigned) n2 
    , cast(num3 as unsigned) n3 
    , cast(num4 as unsigned) n4 
    , cast(num5 as unsigned) n5 
    , cast(num6 as unsigned) n6 
from (
    select t.col 
     , SUBSTRING_INDEX(t.col, '-', 1) str 
     , SUBSTRING_INDEX(t.col, '-', -3) num1 
     , SUBSTRING_INDEX(t.col, '.', -3) num2 
     , SUBSTRING_INDEX(t.col, '-', -2) num3 
     , SUBSTRING_INDEX(t.col, '.', -2) num4 
     , SUBSTRING_INDEX(t.col, '-', -1) num5 
     , SUBSTRING_INDEX(t.col, '.', -1) num6 
    from Table1 t 
) t; 
-- 

|    col | n1 | n2 | n3 | n4 | n5 | n6 | 
|------------------|----|----|----|----|----|----| 
| XYZ-1.0-7.0-1 | 1 | 0 | 7 | 0 | 1 | 0 | 
| XYZ-1.0-27.0-5.7 | 1 | 0 | 27 | 0 | 5 | 7 | 
| XYZ-1.0-20.0-4.6 | 1 | 0 | 20 | 0 | 4 | 6 | 
| XYZ-1.0-10.0-2.4 | 1 | 0 | 10 | 0 | 2 | 4 | 

http://sqlfiddle.com/#!9/d3770/1

使用这些零件FO的排序结果:

select t.col 
from (
    select t.col 
     , SUBSTRING_INDEX(t.col, '-', 1) str 
     , SUBSTRING_INDEX(t.col, '-', -3) num1 
     , SUBSTRING_INDEX(t.col, '.', -3) num2 
     , SUBSTRING_INDEX(t.col, '-', -2) num3 
     , SUBSTRING_INDEX(t.col, '.', -2) num4 
     , SUBSTRING_INDEX(t.col, '-', -1) num5 
     , SUBSTRING_INDEX(t.col, '.', -1) num6 
    from Table1 t 
) t 
order by str 
    , cast(num1 as unsigned) 
    , cast(num2 as unsigned) 
    , cast(num3 as unsigned) 
    , cast(num4 as unsigned) 
    , cast(num5 as unsigned) 
    , cast(num6 as unsigned) 

http://sqlfiddle.com/#!9/d3770/2

你Ç一个也消除子查询:

select t.col 
from Table1 t 
order by SUBSTRING_INDEX(t.col, '-', 1) 
    , cast(SUBSTRING_INDEX(t.col, '-', -3) as unsigned) 
    , cast(SUBSTRING_INDEX(t.col, '.', -3) as unsigned) 
    , cast(SUBSTRING_INDEX(t.col, '-', -2) as unsigned) 
    , cast(SUBSTRING_INDEX(t.col, '.', -2) as unsigned) 
    , cast(SUBSTRING_INDEX(t.col, '-', -1) as unsigned) 
    , cast(SUBSTRING_INDEX(t.col, '.', -1) as unsigned) 

http://sqlfiddle.com/#!9/d3770/5

+0

如果我们添加“XYZ-1.1-7.0-1”,那么它应该在底部。 – steven

+0

@steven - 好的,我意识到这种模式并不像我想象的那么清晰。 “XYZ-1.1-7.0-1”缺少最后一个号码。无论最后一点('.'),我的解决方案将无法正常工作,因为我从右向左计数点数。那么,你的模式是什么? –

+0

由于这个问题,这是我的模式。由“。和 - ”组成。但是以任何数字格式。 – steven

相关问题