Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何从列中按特定格式排序?_Mysql_Sql_Database_Bash - Fatal编程技术网

Mysql 如何从列中按特定格式排序?

Mysql 如何从列中按特定格式排序?,mysql,sql,database,bash,Mysql,Sql,Database,Bash,我有一列包含如下数据: {Population: 1331415} {Population: 44234} {Population: 214124212} {Population: 222222} 我需要按照{Population:]后面的数字和另一个名为name的列对数据库进行排序,它包含使用GNU排序的城市名称,其中-n表示数字排序,-k标志表示根据第二个字段进行排序 一种方法是使用长度和值: order by length(col) desc, col desc 另一种方法是提取数值并

我有一列包含如下数据:

{Population: 1331415}
{Population: 44234}
{Population: 214124212}
{Population: 222222}
我需要按照{Population:]后面的数字和另一个名为name的列对数据库进行排序,它包含使用GNU排序的城市名称

,其中-n表示数字排序,-k标志表示根据第二个字段进行排序


一种方法是使用长度和值:

order by length(col) desc, col desc
另一种方法是提取数值并将其转换为数字:

order by substring_index(col, ': ', -1) + 0 desc
看看这个

            ORDER BY 
            CONVERT( (REPLACE (substring_index(ColumnName, ': ',-1),'}','')), 
            UNSIGNED  INTEGER ) DESC
样本:

        SELECT * ,
        CONVERT((REPLACE(substring_index(A, ': ',-1),'}','')),UNSIGNED  INTEGER )'ORDER'
        FROM (
            SELECT '{Population: 1331415}' AS A UNION
            SELECT '{Population: 44234}' UNION
            SELECT '{Population: 214124212}' UNION
            SELECT '{Population: 222222}'
            )B 
        ORDER BY 
        CONVERT( (REPLACE (substring_index(A, ': ',-1),'}','')), UNSIGNED  INTEGER )
        DESC
输出:


首先,你为什么要这样存储数据。它可以存储在两个不同的列中,或者如果人口是唯一的值,那么它可以作为一个列来存储。这是一个普通文件,你可以使用外部shell实用程序还是只使用本机sql查询?只是为了了解要使用哪种语言:你尝试了什么?当我尝试使用函数时比如说这个函数没有安装,我是从一个terminal@GordonLinoff-很高兴添加0以消除行末尾的}-投票up@BerndBuffen-我认为应该将数据隐式转换为int以避免字符串ordering@RazvanBuzatu . . . length和substring_index都是MySQL的本机函数。您真正使用的是什么数据库?如果需要,还可以添加sort-V inputfile。
        SELECT * ,
        CONVERT((REPLACE(substring_index(A, ': ',-1),'}','')),UNSIGNED  INTEGER )'ORDER'
        FROM (
            SELECT '{Population: 1331415}' AS A UNION
            SELECT '{Population: 44234}' UNION
            SELECT '{Population: 214124212}' UNION
            SELECT '{Population: 222222}'
            )B 
        ORDER BY 
        CONVERT( (REPLACE (substring_index(A, ': ',-1),'}','')), UNSIGNED  INTEGER )
        DESC