Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 基于现有字段的第一部分填充新字段_Mysql_Sql_String_Sql Update - Fatal编程技术网

Mysql 基于现有字段的第一部分填充新字段

Mysql 基于现有字段的第一部分填充新字段,mysql,sql,string,sql-update,Mysql,Sql,String,Sql Update,我正在从事一个数据库项目,其中一项任务是用现场课程id的第一部分(HarvardX/CB22x/2013_Spring)填充一个新创建的现场“机构”。所以我需要用第一部分“HarvardX”填充institution字段。如何在MySQL workbench中做到这一点?您可以使用子字符串\u index(): 如果还要提取中间零件和末端零件,则: update mytable set institution = substring_index(course_id, '/', 1),

我正在从事一个数据库项目,其中一项任务是用现场课程id的第一部分(HarvardX/CB22x/2013_Spring)填充一个新创建的现场“机构”。所以我需要用第一部分“HarvardX”填充institution字段。如何在MySQL workbench中做到这一点?

您可以使用
子字符串\u index()

如果还要提取中间零件和末端零件,则:

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)

我收到以下错误消息“error code 1193 unknow system variable‘institution’@AmandaRamos:您的问题提到了一个名为
institution
的列,您的表中没有这样的列吗?它现在可以工作了,我忘了添加update mytable:)如果我想用HarvardX/CB22x/2013_Spring的第二部分将另一个字段更新为CB22x,第三个2013_Spring的位置是什么?@AmandaRamos:你应该从一开始就问这个问题。。。不过,请参阅我的更新。
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)