简体   繁体   English

将值从一列更改为另一列

[英]Change value from one column into another

I have got a table: 我有一张桌子:

ID        | Description
--------------------
1.13.1-3  | .1 Hello
1.13.1-3  | .2 World
1.13.1-3  | .3 Text
4.54.1-4  | sthg (.1) Ble
4.54.1-4  | sthg (.2) Bla
4.54.1-4  | aaaa (.3) Qwer
4.54.1-4  | bbbb (.4) Tyuio

And would like to change ending of ID by taking value from second column to have result like: 并希望通过从第二列获取值来更改ID的结尾,结果如下:

ID       | Description
--------------------
1.13.1   | Hello
1.13.2   | World
1.13.3   | Text
4.54.1   | Ble
4.54.2   | Bla
4.54.3   | Qwer
4.54.4   | Tyuio

Is there any quick way to do it in postgresql? 在postgresql中有什么快速的方法吗?

Use regex to manipulate the strings into what you want: 使用正则表达式将字符串操纵为所需的字符串:

update mytable set
ID = regexp_replace(ID, '\.[^.]*$', '') || substring(Description from '\.[0-9+]'),
Description = regexp_replace(Description, '.*\.[0-9]+\S* ', '')

See SQLFiddle showing this query working with your data. 请参阅SQLFiddle,其中显示此查询与您的数据一起使用。

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

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