将postgresql表列值分为多列
我正在与PostgreSQL一起工作,并且有一个带有2列的表Manager_id
和Manager_name
。我想拆分manager_name
列(带有诸如:盎格鲁东部船舶管理 - 中国香港)的值|城市|国家。定界器“ - ”和“,”。
我尝试了此查询,但它只是显示了结果,而不是永久保存的列
select
manager_name,
split_part(manager_name, '-', 1) m_name,
split_part(manager_name, ',', 2) city,
split_part(manager_name, ',', 2) country
from
manager2
是否有其他方法可以将本列分开并将其永久保存在该表中?
I am working with PostgreSQL and I have a table with 2 columns manager_id
and manager_name
. I want to split manager_name
column (with values such as: ANGLO EASTERN SHIPMANAGEMENT - HONG KONG, CHINA) to Manager name| city | country. delimiter "-"and "," .
I tried this query but it just show the result, not saved that columns permanently
select
manager_name,
split_part(manager_name, '-', 1) m_name,
split_part(manager_name, ',', 2) city,
split_part(manager_name, ',', 2) country
from
manager2
Is there any other way to split this column and saved it in that table permanently ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
也许有点冗长,但以下内容似乎是这样做的。您的
split_part()
从Manager_name
检索城市需要进行一些调整;还将它们包裹在trim()
中以删除前导和落后空间。更新后表:
更新以处理诸如
psa之类的案例| null |新加坡
。您可以使用以下代码:结果:而不是上面使用的CTE(),
结果:结果:
Perhaps a bit verbose, but the following seems to do it. Your
SPLIT_PART()
to retrieve the city frommanager_name
needed some tweaking; also wrap them inTRIM()
to remove leading and trailing spaces.Table after update:
Update to handle cases like 'PSA - SINGAPORE' as
PSA | NULL | SINGAPORE
. Instead of the CTE used above (WITH results AS (...)
), you could use the following code:Result:
您可以通过我知道的两种方式来执行此操作。您可以使用split_part和alter表或创建视图语句将查询永久存储在另一个表中。然后将表用作主表作为创建视图的
创建
三列将在您的视图中创建三列,然后您可以删除拆分的原始Manager_name。
You can do this in two ways I know. You can use the SPLIT_PART and ALTER TABLE or the CREATE VIEW statement to store the query permanently inside another table. Then use the table as your primary table
The Create View
Three columns will be created in your view, then you can drop the original manager_name that you split.