简体   繁体   English

拆分一列为2列mysql表

[英]Split a column into 2 columns mysql table

I have this column in mysql table:我在 mysql 表中有此列:

LOT_LOCATION
SGBAKE.0013
SGHAST.0008Z1
SGHAST.0011ZU

How to split to this table[MANAGED TO DO SO BUT DK HOW TO CHANGE THE TABLE ITSELF):如何拆分到此表[设法做到这一点,但 DK 如何更改表本身]:

LOT_LOCATION, Zone Attribute
SGBAKE.0013, ''
SGHAST.0008, Z1
SGHAST.0011, ZU

Any help is appreciated thanks!任何帮助表示感谢谢谢!

my code only select 2 columns but does not alter the table and I dont know how to put condition in creation and alteration of columns:我的代码只有 select 2 列,但没有改变表格,我不知道如何在创建和改变列时设置条件:

select if(locate('Z',LOT_LOCATION)=0,LOT_LOCATION,substring_index(LOT_LOCATION, 'Z', 1)), 
       if(locate('Z',LOT_LOCATION)=0,'',substring_index(LOT_LOCATION, 'Z', -1)) 
            As Zone_Attribute 
from skynet_msa.Lab_WIP_History;

I tried this UPDATE but suddenly the zone attribute column values disappear我尝试了这个 UPDATE 但区域属性列值突然消失了

UPDATE Lab_WIP_History
    SET LOT_LOCATION = if(locate('Z',LOT_LOCATION)=0,LOT_LOCATION,substring_index(LOT_LOCATION, 'Z', 1)), 
    `Zone Attribute` = if(locate('Z',LOT_LOCATION)=0,'',substring_index(LOT_LOCATION, 'Z', -1))

lot_location is updated before zone_attribute - ie the zone_attribute test finds no z in lot_location btw your select query does not produce the result you claim lot_location 在 zone_attribute 之前更新 - 即 zone_attribute 测试在 lot_location 中找不到 z 顺便说一句,您的 select 查询不会产生您声称的结果

reverse the order of the set statement反转 set 语句的顺序

UPDATE Lab_WIP_History
    SET `Zone Attribute` = if(locate('Z',LOT_LOCATION)=0,'',substring_index(LOT_LOCATION, 'Z', -1)), 
     LOT_LOCATION = if(locate('Z',LOT_LOCATION)=0,LOT_LOCATION,substring_index(LOT_LOCATION, 'Z', 1))
;    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM