MySQL(通常是 SQL)限制列

标签 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 列)。

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

最佳答案

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

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

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

关于MySQL(通常是 SQL)限制列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30182303/

相关文章:

php - LEFT JOIN 或 REGULAR JOIN,以及如何将 MySql 表与数组进行比较?需要表现!

php - 如何在mysql中删除表中的记录但保留前x条记录

mysql - 'show table status' 中的数据长度

machine-learning - python中适合稀疏高维特征的理想分类器(具有层次分类)

mysql - 使用 gcloud 命令行创建第二代 Cloud SQL

mysql - 在 SQL 中查询 JSON 字符串

r - 大稀疏矩阵到三角矩阵 R

sql-server - SQL Server : how to populate sparse data with the rest of zero values?

java - 如何在 SparseDoubleMatrix2D (Java Colt 库)中找到最大值?甚至在一维矩阵中

python - for-loop + numpy.where 的向量化版本