[英]Split a column into 2 columns mysql table
我在 mysql 表中有此列:
LOT_LOCATION
SGBAKE.0013
SGHAST.0008Z1
SGHAST.0011ZU
如何拆分到此表[設法做到這一點,但 DK 如何更改表本身]:
LOT_LOCATION, Zone Attribute
SGBAKE.0013, ''
SGHAST.0008, Z1
SGHAST.0011, ZU
任何幫助表示感謝謝謝!
我的代碼只有 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;
我嘗試了這個 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 在 zone_attribute 之前更新 - 即 zone_attribute 測試在 lot_location 中找不到 z 順便說一句,您的 select 查詢不會產生您聲稱的結果
反轉 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.