Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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/8/xslt/3.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 select上的联接、重命名和清除(压缩)列_Mysql_Sql - Fatal编程技术网

Mysql sql select上的联接、重命名和清除(压缩)列

Mysql sql select上的联接、重命名和清除(压缩)列,mysql,sql,Mysql,Sql,在excel上,我查阅MySQL服务器上的一个表,然后修改数据的显示格式。我目前正在更改SQL,因此数据已经按照我想要的方式出现,其中一部分如下所示: 有25列名为operator1-5lot1-5(1-5如1到5所示): operator1lot1,operator2lot1,operator3lot1,operator4lot1,operator1lot2,operator2lot2,operator3lot2,operator4lot2,operator5lot2,operator1lot

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

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

operator1lot1
operator2lot1
operator3lot1
operator4lot1
operator1lot2
operator2lot2
operator3lot2
operator4lot2
operator5lot2
operator1lot3
operator3lot3
operator4lot3
operator5lot3
operator1lot4
operator2lot4
operator4lot4
operator5lot4
operator1lot5
operator2lot5
operator4lot5
运算符5lot5

operator1lot1
始终有一个值,如果后面的操作员也有一个值(如果
operator4lot1
有一个值,则第1批的操作员1到3也有一个值),则下一个操作员可以有一个值,方法与operator1lotX相同(如果
operator 1Lot3
有一个值,则标段1和标段2的操作员1也有一个值,但这并不意味着标段1和标段2的操作员2到5有一个值)

(没有值意味着它是
NULL


当前在excel I
上选择所有25列,然后在有空格的情况下将值连接到左侧(如果
运算符2lot3
NULL
则将
运算符3lot3
的值移到左侧),然后删除空列(其所有值均为
NULL
)从表的右侧到左侧,最后我从左到右将列重命名为
1
x
(2),正如我之前所述,使用生成实际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列或更多就不难了

第一步 首先,我们需要组合所有列,因为我们可以使用
CONCAT\WS
,此函数的第一个参数是我们将在本例中使用的胶水

步骤2 现在我们需要以某种方式分解
上的这些值,
使用
子字符串索引
我们可以在指定的引用上分离指定字符端的值

步骤3 在通过第一次出现后,我们需要获取的最后一次出现,在本例中是第二次出现,因此我们再次执行相同的函数,但现在的值为负值
-1

步骤4 最后一部分是检查实际是否有足够的值可供检查。这一部分统计使用的
,如果有足够的值,则执行代码,否则只需将值设置为
NULL


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

举个例子,仅考虑前3列和前3行数据,规范化模式可能由4或5列组成,如下所示:

id operator lot entry value
 1        1   1     1  117
 2        1   1     2  117 
 3        1   1     3  129 
 4        2   1     1  319
 5        2   1     2  319
 6        3   1     2  530

代理主键id不是严格必需的,因为在接下来的3列中存在一个令人满意的PK。

这不是一个数据库表。这是一个spreadhseet:-(如何获取此数据?对我来说,在
insert
update
@草莓上应用此id更有意义,因为它是MySQL服务器上的表(使用程序HeidiSQL查看)@PeterM From Excel我连接到数据库并使用
选择
。哦,它可能在数据库上。但它不是一个数据库表。至少,数据库表通常会标准化为3NF。这不是标准化的。如果这是您想要做的事情,我们可能会帮助您解决,但我们需要知道为什么,例如,'338''出现在'303'旁边,而不是'129'。另一方面,如果您不想修复数据库架构的结构,那么这实际上不是一个mysql/sql问题。
substring_index(`combinedCols`,',',1 )
substring_index(substring_index(`combinedCols`,',',2 ),',',-1)
CASE WHEN (char_length(`combinedCols`) - char_length(replace(`combinedCols`, ',', '')) + 1) > 1
id operator lot entry value
 1        1   1     1  117
 2        1   1     2  117 
 3        1   1     3  129 
 4        2   1     1  319
 5        2   1     2  319
 6        3   1     2  530