SQL查询,按两个子字符串排序

时间:2015-05-15 17:48:37

标签: mysql sorting substring

我希望根据第二个数字和第三个数字对列中的所有内容进行排序。

例如:

1.197.980大于 1.191.5061

我目前拥有的当前SQL查询如下。

var express = require( 'express' );
var passport = require( 'passport' );
var session = require( 'express-session' );
var RDBStore = require( 'express-session-rethinkdb' )( session );

我得到以下输出。

    if ( req.isUnauthenticated() ) {

        // you are not even logged in, wtf
        res.redirect( '/' );

        return;

    }

    var sessionCookie = req.cookies['connect.sid'];

    if ( ! sessionCookie ) {

        // nothing to do here
        res.redirect( '/' );

        return;

    }

    var sessionId = sessionCookie.split( '.' )[0].replace( 's:', '' );

    thinky.r.db( 'test' ).table( 'session' ).get( sessionId ).delete().run().then( function( result ) {

        if ( ! result.deleted ) {

            // we did not manage to find session for this user
            res.redirect( '/' );

            return;

        }

        req.logout();
        res.redirect( '/' );

        return;

    });

});

所以我非常希望按照第一个“之后的数字排序”。然后在第二个句号“。”之后排序。我希望四位数高于三位数。我希望这一切都有道理。 :)

所以我应该有以下内容:

SELECT av_sig_ver
,CONVERT(SUBSTRING_INDEX(av_sig_ver,'.',-2),UNSIGNED INTEGER) AS 'av_sig_ver'
FROM `test`.`test_fep_signatures`
ORDER BY av_sig_ver DESC
LIMIT 50;

编辑:已解决 谢谢你的建议。让我思考正确的方向。这是我对它的解决方法。 :)

+--------------+------------+
| av_sig_ver   | av_sig_ver |
+--------------+------------+
| 1.191.5061.0 |       5061 |
| 1.191.5061.0 |       5061 |
| 1.191.5061.0 |       5061 |
| 1.191.5061.0 |       5061 |
| 1.191.5015.0 |       5015 |
| 1.191.5015.0 |       5015 |
| 1.191.4993.0 |       4993 |
| 1.191.4993.0 |       4993 |
| 1.191.4960.0 |       4960 |
| 1.191.4710.0 |       4710 |
| 1.191.4634.0 |       4634 |
| 1.191.4634.0 |       4634 |
| 1.191.4634.0 |       4634 |
| 1.191.4588.0 |       4588 |
| 1.195.3814.0 |       3814 |
| 1.195.3814.0 |       3814 |
| 1.195.3814.0 |       3814 |
| 1.195.3814.0 |       3814 |
| 1.195.3814.0 |       3814 |
| 1.195.3814.0 |       3814 |
| 1.195.3814.0 |       3814 |
| 1.195.3814.0 |       3814 |
| 1.195.3797.0 |       3797 |
| 1.195.3797.0 |       3797 |
| 1.195.3797.0 |       3797 |
| 1.195.3764.0 |       3764 |
| 1.195.3764.0 |       3764 |
| 1.195.3764.0 |       3764 |
| 1.195.3764.0 |       3764 |
| 1.195.3741.0 |       3741 |
| 1.195.3711.0 |       3711 |
| 1.195.3623.0 |       3623 |
| 1.195.3600.0 |       3600 |
| 1.195.3600.0 |       3600 |
| 1.195.3600.0 |       3600 |
| 1.195.3600.0 |       3600 |
| 1.195.3510.0 |       3510 |
| 1.193.3509.0 |       3509 |
| 1.193.3509.0 |       3509 |
| 1.193.3509.0 |       3509 |
| 1.193.3509.0 |       3509 |
| 1.193.3509.0 |       3509 |
| 1.193.3485.0 |       3485 |
| 1.193.3485.0 |       3485 |
| 1.195.3480.0 |       3480 |
| 1.195.3480.0 |       3480 |
| 1.191.3480.0 |       3480 |
| 1.195.3480.0 |       3480 |
| 1.195.3480.0 |       3480 |
| 1.195.3480.0 |       3480 |
+--------------+------------+
50 rows in set, 11160 warnings (0.01 sec)

1 个答案:

答案 0 :(得分:0)

SELECT av_sig_ver
,CONVERT(SUBSTRING_INDEX(av_sig_ver,'.',-2),UNSIGNED INTEGER) AS 'one_av_sig_ver'
,CONVERT(SUBSTRING_INDEX(av_sig_ver,'.',-3),UNSIGNED INTEGER) AS 'two_av_sig_ver'
,COUNT(NULLIF(TRIM(av_sig_ver), '')) AS 'total'
FROM `test`.`test_fep_signatures`
GROUP by av_sig_ver
ORDER BY two_av_sig_ver DESC, one_av_sig_ver DESC;