MySQL按其整数对varchar进行排序

时间:2016-01-20 04:02:07

标签: mysql sql varchar

如何对列(VARCHAR)进行排序:

[CSGO] Bot #1
[CSGO] Bot #2
[CSGO] Bot #3
...
[CSGO] Bot #10

我的查询结果为:

[CSGO] Bot #2
[CSGO] Bot #23
[CSGO] Bot #5
[CSGO] Bot #6

查询:

SELECT bot_id, name, username FROM bots ORDER BY ABS(REPLACE(name, '[CSGO] #', '')) ASC

没有ABS()和REPLACE(),给出的结果基本相同。

3 个答案:

答案 0 :(得分:3)

假设前缀长度相同,这是一种简单的方法:

order by length(name), name

如果你只想在#:

之后的部分去
order by substring_index(name, '#', -1) + 0

答案 1 :(得分:1)

SELECT bot_id, name, username 
FROM bots 
ORDER BY substr(name, 13) + 0

如果您的列名始终以'[CSGO] Bot#'开头,请执行以下操作:

(function () {
    'use strict';

    angular
        .module('app.conversation')
        .factory('conversationFactory', conversation);

    conversation.$inject = ['$rootScope', '$q', 'authFactory', '$location'];

    function conversation($rootScope, $q, authFactory, $location) {

        var service = {
            changeLocation: changeLocation
        };

        return service;

        function changeLocation() {
            $location.url( "/messages" ); // recarga MIERDA!!!!!
        }

    }

})();

答案 2 :(得分:0)

 ORDER BY ABS(REPLACE(name, '[CSGO] Bot #', ''))*1 ASC