MySQL(一般的SQL)限制列

时间:2015-05-12 05:14:12

标签: mysql sparse-matrix

如果没有正确的短语,很难描述这个,所以我举一个例子。请考虑以下数据:

colA     colB      colC      colD      colE
------------------------------------------------
Hello     from                        
          this      is        
Can                           i         
                                        please?
But       also      all       columns   pop'd

如您所见,许多列实际上都是空的。最后,我想获得以下输出:

col1     col2      col3      col4      col5
------------------------------------------------
Hello     from
this      is
Can       i
please?
But       also      all       columns   pop'd

基本上,数据向左移动。我想过使用CASE WHEN语句,但这变得非常复杂,因为它实际上是7列(不是上面例子中概述的5列)。

获得该结果的最有效方法是什么。结果列的名称无关紧要,无论如何都会重命名。

1 个答案:

答案 0 :(得分:0)

将其视为思想练习,因为它似乎最好留给更好的结构,或者在应用程序级别完成 - 你可以用一个单一的,可怕的丑陋查询完成你想要的东西,如下所示: / p>

SELECT substring_index(substring_index(CONCAT (
                trim(leading '#@' FROM trim(trailing '#@' FROM replace(replace(replace(concat_ws('#@', cola, colb, colc, cold, cole), '#@#@', '#@~!'), '~!#@', ''), '~!', ''))),
                '#@'
                ), '#@', 1), '#@', - 1) col1,
    substring_index(substring_index(CONCAT (
                trim(leading '#@' FROM trim(trailing '#@' FROM replace(replace(replace(concat_ws('#@', cola, colb, colc, cold, cole), '#@#@', '#@~!'), '~!#@', ''), '~!', ''))),
                '#@'
                ), '#@', 2), '#@', - 1) col2,
    substring_index(substring_index(CONCAT (
                trim(leading '#@' FROM trim(trailing '#@' FROM replace(replace(replace(concat_ws('#@', cola, colb, colc, cold, cole), '#@#@', '#@~!'), '~!#@', ''), '~!', ''))),
                '#@'
                ), '#@', 3), '#@', - 1) col3,
    substring_index(substring_index(CONCAT (
                trim(leading '#@' FROM trim(trailing '#@' FROM replace(replace(replace(concat_ws('#@', cola, colb, colc, cold, cole), '#@#@', '#@~!'), '~!#@', ''), '~!', ''))),
                '#@'
                ), '#@', 4), '#@', - 1) col4,
    substring_index(substring_index(CONCAT (
                trim(leading '#@' FROM trim(trailing '#@' FROM replace(replace(replace(concat_ws('#@', cola, colb, colc, cold, cole), '#@#@', '#@~!'), '~!#@', ''), '~!', ''))),
                '#@'
                ), '#@', 5), '#@', - 1) col5
FROM yourtable;

它的要点是,我们创建一个单独的分隔字符串,其中包含使用concat_ws的所有列(我使用#@作为分隔符 - 它可以是任何内容。这个想法是不可能的出现在你的字符串中)。然后,我们通过一系列replace调用,将分隔符的所有多次出现转换为单个出现。通过两次调用trim,我们从字符串的前端和末尾删除分隔符,然后使用concat将其添加回所有字符串的末尾。然后,我们可以使用substring_index在字符串中的每个位置提取单词。如果该字符串中没有更多单词,则尾随分隔符可确保我们可以获得空结果。对每个列重复此操作一次,将偏移量增加到内部substring_index调用,具体取决于您要创建的虚拟列。

demo here

如果要将其扩展到七列,只需再重复两次该字段,并每次增加偏移量。