根据现有字段的第一部分填充新字段 [英] populate a new field based on the first portion of an existing field

查看:38
本文介绍了根据现有字段的第一部分填充新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个数据库项目,其中一项任务是使用字段 course_id 的第一部分填充新创建的字段机构",即HarvardX/CB22x/2013_Spring.所以我需要用第一部分填充机构字段'HarvardX.我怎样才能在 MySQL 工作台中做到这一点?

I am working on a database project and one of the tasks is to populate a newly created field 'institution' with the first portion of the field course_id which is HarvardX/CB22x/2013_Spring. So I need to populate the institution field with the first portion only 'HarvardX. How can I do that in MySQL workbench?

推荐答案

您可以使用 substring_index():

update mytable
set institution = substring_index(course_id, '/', 1)

如果您还想提取中间部分和结尾部分,则:

If you want to also extract the middlde part and end part, then:

update mytable
set 
    institution = substring_index(course_id, '/', 1),
    other_col   = substring_index(substring(course_id, locate('/', course_id) + 1), '/', 1),
    more_col    = substring_index(course_id, '/', -1)

这篇关于根据现有字段的第一部分填充新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆