在 sql select 上加入、重命名和清除(紧凑)列

用户7393973

在 excel 上,我从 MySQL 服务器中查阅了一个表,然后修改了数据显示方式的格式。我目前正在更改 SQL,因此数据已经按照我想要的方式出现,其中一部分是这样的:

有 25 列名为 operator1-5lot1-5(1-5 为 1 到 5),如下所示:

operator1lot1operator2lot1operator3lot1operator4lot1operator5lot1operator1lot2operator2lot2operator3lot2operator4lot2operator5lot2operator1lot3operator2lot3operator3lot3operator4lot3operator5lot3operator1lot4operator2lot4operator3lot4operator4lot4operator5lot4operator1lot5operator2lot5operator3lot5operator4lot5operator5lot5

operator1lot1总是有一个值,那么每个下一个操作符都可以有一个值,如果后面的操作符也有(如果operator4lot1有一个值,那么第 1 批的操作符 1 到 3也有)和 operator1lotX 的相同方式(如果operator1lot3有一个值,那么操作符 1批次 1 和 2,但并不意味着批次 1 和 2 的运算符 2 至 5 具有值)。

(没有价值意味着它是NULL

目前,在Excel中我SELECT的所有25列,然后加入值到左侧,如果有空间(如果operator2lot3NULL的值operator3lot3被移动到那里,向左),那么我会删除那些空列(即有它的所有值NULL)从表的右侧到左侧,最后我将从左侧到右侧的列重命名1x(2 <= x <= 25)。

这里有一个例子可以更好地解释它。数据库表如下所示:

MySQL表

在 Excel 上,它的格式为:

Excel表格

我的问题是是否可以直接在 SQL 中格式化数据。我不知道很多 SQL 命令,所以我能找到的最接近的不是我想要的东西是CONCAT_WSAS

彼得·M

正如我之前所说,使用生成实际 Excel 文件的脚本可能更容易做到这一点。

无论如何,我确实有一个解决方案,它看起来并不漂亮,我不确定这是理想的方法,但就这样吧。

SELECT 
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 0 
        THEN substring_index(`combinedCols`,',',1 )
        ELSE NULL 
    END AS col1,
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 1 
        THEN substring_index(substring_index(`combinedCols`,',',2 ),',',-1) 
        ELSE NULL 
    END AS col2,
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 2 
        THEN substring_index(substring_index(`combinedCols`,',',3 ),',',-1)
        ELSE NULL 
    END AS col3,
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 3 
        THEN substring_index(substring_index(`combinedCols`,',',4 ),',',-1)
        ELSE NULL 
    END AS col4,
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 4 
        THEN substring_index(substring_index(`combinedCols`,',',5 ),',',-1)
        ELSE NULL 
    END AS col5,
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 5 
        THEN substring_index(substring_index(`combinedCols`,',',6 ),',',-1)
        ELSE NULL 
    END AS col6,
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 6 
        THEN substring_index(substring_index(`combinedCols`,',',7 ),',',-1)
        ELSE NULL 
    END AS col7,
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 7 
        THEN substring_index(substring_index(`combinedCols`,',',8 ),',',-1)
        ELSE NULL 
    END AS col8,
    CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 8 
        THEN substring_index(substring_index(`combinedCols`,',',9 ),',',-1)
        ELSE NULL 
    END AS col9
FROM 
    (SELECT CONCAT_WS(',',`col1`,`col2`,`col3`,`col4`,`col5`,`col6`,`col7`,`col8`,`col9`) AS `combinedCols` FROM `tableName`) AS `tableNameAlias`;

在我的示例中,我只将它放置了 9 列,但是一旦您理解了我所做的事情,将其扩展到 25 列或更多应该不会太难。

步骤1

SELECT CONCAT_WS(',',`col1`,`col2`,`col3`,`col4`,`col5`,`col6`,`col7`,`col8`,`col9`) AS `combinedCols` FROM `tableName`

首先我们需要组合所有列,因为我们可以使用CONCAT_WS,这个函数的第一个参数是我们将在这种情况下使用的胶水,

第2步

substring_index(`combinedCols`,',',1 )

现在,我们需要以某种方式爆发在这些值,使用SUBSTRING_INDEX,我们可以在指定发生指定的字符结束分隔值。

第 3 步

substring_index(substring_index(`combinedCols`,',',2 ),',',-1)

在通过第一次出现后,我们需要获取最后一次出现的 ,在这种情况下,第二次出现,因此我们再次执行相同的函数,但现在使用负值-1

第四步

CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 1

最后一部分是检查实际上是否有足够的值来检查。这部分计算使用的数量,,如果足够执行代码,否则只需设置一个值NULL

那么希望这能解决你的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章