<bdo id='rPViq'></bdo><ul id='rPViq'></ul>
  • <small id='rPViq'></small><noframes id='rPViq'>

    <tfoot id='rPViq'></tfoot>
    <i id='rPViq'><tr id='rPViq'><dt id='rPViq'><q id='rPViq'><span id='rPViq'><b id='rPViq'><form id='rPViq'><ins id='rPViq'></ins><ul id='rPViq'></ul><sub id='rPViq'></sub></form><legend id='rPViq'></legend><bdo id='rPViq'><pre id='rPViq'><center id='rPViq'></center></pre></bdo></b><th id='rPViq'></th></span></q></dt></tr></i><div id='rPViq'><tfoot id='rPViq'></tfoot><dl id='rPViq'><fieldset id='rPViq'></fieldset></dl></div>

      <legend id='rPViq'><style id='rPViq'><dir id='rPViq'><q id='rPViq'></q></dir></style></legend>

      1. 在 SQL 中对包含数字的字符串列进行排序?

        时间:2023-06-26

        <legend id='hUjLB'><style id='hUjLB'><dir id='hUjLB'><q id='hUjLB'></q></dir></style></legend><tfoot id='hUjLB'></tfoot>

        • <bdo id='hUjLB'></bdo><ul id='hUjLB'></ul>
                <tbody id='hUjLB'></tbody>
            • <i id='hUjLB'><tr id='hUjLB'><dt id='hUjLB'><q id='hUjLB'><span id='hUjLB'><b id='hUjLB'><form id='hUjLB'><ins id='hUjLB'></ins><ul id='hUjLB'></ul><sub id='hUjLB'></sub></form><legend id='hUjLB'></legend><bdo id='hUjLB'><pre id='hUjLB'><center id='hUjLB'></center></pre></bdo></b><th id='hUjLB'></th></span></q></dt></tr></i><div id='hUjLB'><tfoot id='hUjLB'></tfoot><dl id='hUjLB'><fieldset id='hUjLB'></fieldset></dl></div>

              1. <small id='hUjLB'></small><noframes id='hUjLB'>

                  本文介绍了在 SQL 中对包含数字的字符串列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试对字符串列(包含数字)进行排序.

                  I am trying to sort string column (containing numbers).

                  // SELECT `name` FROM `mytable` ORDER BY `name` ASC
                  +----------+
                  +-- name --+
                  +----------+
                  +-- a 1 ---+
                  +-- a 12 --+
                  +-- a 2 ---+
                  +-- a 3 ---+
                  

                  你看到Mysql的自然排序算法在a 1之后放置a 12(这对大多数应用程序来说都可以),但我有独特的需求,所以我希望结果应该这样排序.

                  You see natural sorting algorithm of Mysql is placing a 12 after a 1 (which is ok for most apps), But I have unique needs, so I want result should be sorted like this.

                  +----------+
                  +-- name --+
                  +----------+
                  +-- a 1 ---+
                  +-- a 2 ---+
                  +-- a 3 ---+
                  +-- a 12 --+
                  

                  是否可以仅使用 SQL,或者我必须在应用程序级别操作结果集?

                  Is it possible with just SQL, or I have to manipulate result-set at application level?

                  推荐答案

                  继续假设它总是 WORD_space_NUMBER 这应该有效:

                  Going on the assumption it's always WORD_space_NUMBER this should work:

                  SELECT   *
                  FROM     table
                  ORDER BY CAST(SUBSTRING(column,LOCATE(' ',column)+1) AS SIGNED)
                  

                  使用POSITION找到空间,SUBSTRING 抓取后面的数字,CAST 使其成为可比较的值.

                  Use POSITION to find the space, SUBSTRING to grab the number after it, and CAST to make it a comparable value.

                  如果该列有不同的模式,请告诉我,我会尝试设计出更好的解决方法.

                  If there is a different pattern to the column, let me know and I'll try to devise a better work-around.

                  编辑已证明有效:

                  mysql> INSERT INTO t (st) VALUES ('a 1'),('a 12'),('a 6'),('a 11');
                  Query OK, 4 rows affected (0.00 sec)
                  Records: 4  Duplicates: 0  Warnings: 0
                  
                  mysql> SELECT * FROM t ORDER BY st;
                  +----+------+
                  | id | st   |
                  +----+------+
                  |  1 | a 1  |
                  |  4 | a 11 |
                  |  2 | a 12 |
                  |  3 | a 6  |
                  +----+------+
                  4 rows in set (0.00 sec)
                  
                  mysql> SELECT * FROM t ORDER BY CAST(SUBSTRING(st,LOCATE(' ',st)+1) AS SIGNED);
                  +----+------+
                  | id | st   |
                  +----+------+
                  |  1 | a 1  |
                  |  3 | a 6  |
                  |  4 | a 11 |
                  |  2 | a 12 |
                  +----+------+
                  
                  mysql> INSERT INTO t (st) VALUES ('b 1'),('b 12'),('b 6'),('b 11');
                  Query OK, 4 rows affected (0.00 sec)
                  Records: 4  Duplicates: 0  Warnings: 0
                  
                  mysql> SELECT * FROM t ORDER BY CAST(SUBSTRING(st,LOCATE(' ',st)+1) AS SIGNED);
                  +----+------+
                  | id | st   |
                  +----+------+
                  |  1 | a 1  |
                  |  5 | b 1  |
                  |  3 | a 6  |
                  |  7 | b 6  |
                  |  4 | a 11 |
                  |  8 | b 11 |
                  |  2 | a 12 |
                  |  6 | b 12 |
                  +----+------+
                  8 rows in set (0.00 sec)
                  
                  mysql> SELECT * FROM t ORDER BY LEFT(st,LOCATE(' ',st)), CAST(SUBSTRING(st,LOCATE(' ',st)+1) AS SIGNED);
                  +----+------+
                  | id | st   |
                  +----+------+
                  |  1 | a 1  |
                  |  3 | a 6  |
                  |  4 | a 11 |
                  |  2 | a 12 |
                  |  5 | b 1  |
                  |  7 | b 6  |
                  |  8 | b 11 |
                  |  6 | b 12 |
                  +----+------+
                  8 rows in set (0.00 sec)
                  

                  忽略我蹩脚的表/列名称,但给了我正确的结果.还更进一步,添加了双重排序以将字母前缀与数字分开.

                  ignore my lame table/column names, but gives me the correct result. Also went a little further and added double sort to break letters prefix with numeric.

                  编辑SUBSTRING_INDEX 将使它更具可读性.

                  ORDER BY SUBSTRING_INDEX(st, " ", 1) ASC, CAST(SUBSTRING_INDEX(st, " ", -1) AS SIGNED)
                  

                  这篇关于在 SQL 中对包含数字的字符串列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何修复损坏的 xampp 'mysql.user' 表? 下一篇:使用 MySQL JSON 字段加入表

                  相关文章

                    <i id='S0Lgb'><tr id='S0Lgb'><dt id='S0Lgb'><q id='S0Lgb'><span id='S0Lgb'><b id='S0Lgb'><form id='S0Lgb'><ins id='S0Lgb'></ins><ul id='S0Lgb'></ul><sub id='S0Lgb'></sub></form><legend id='S0Lgb'></legend><bdo id='S0Lgb'><pre id='S0Lgb'><center id='S0Lgb'></center></pre></bdo></b><th id='S0Lgb'></th></span></q></dt></tr></i><div id='S0Lgb'><tfoot id='S0Lgb'></tfoot><dl id='S0Lgb'><fieldset id='S0Lgb'></fieldset></dl></div>

                        <bdo id='S0Lgb'></bdo><ul id='S0Lgb'></ul>
                      <legend id='S0Lgb'><style id='S0Lgb'><dir id='S0Lgb'><q id='S0Lgb'></q></dir></style></legend>
                      <tfoot id='S0Lgb'></tfoot>

                      <small id='S0Lgb'></small><noframes id='S0Lgb'>