Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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(一般为SQL)限制列_Mysql_Sparse Matrix - Fatal编程技术网

MySQL(一般为SQL)限制列

MySQL(一般为SQL)限制列,mysql,sparse-matrix,Mysql,Sparse Matrix,如果没有正确的短语,很难描述这一点,因此我将举一个例子。考虑以下数据: colA colB colC colD colE ------------------------------------------------ Hello from this is Can i

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

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列)


获得这一结果的最有效方法是什么。结果列的名称无关紧要,它们在任何情况下都会被重命名。

将其视为一种思考练习,因为它似乎最好留给一个更好的结构,或者在应用程序级别完成—您可以通过一个可怕的查询来完成您想要的,如下所示:

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\u index
提取字符串中每个位置的单词。尾随分隔符确保如果该字符串中没有其他单词,则可以获得空结果。对每一列重复此操作一次,根据要创建的虚拟列,将偏移量增加到内部
子字符串\u index
调用


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

是否每行最多只有两个值?否,所有七列也可能被填充。。。我将改编这个例子。那么就没有简单的问题了如果我们能将他们限制在。。说。。。3?如果有一个输入行的七个值都已填充,您希望有多少输出列?最好不要考虑以下问题:-(