嵌套查询中的更新语句

时间:2016-08-26 08:57:50

标签: mysql

我有更新查询。但它显示sql错误

查询

Update ms_student set student_first_name=y.student_first_name,student_middle_name=y.student_middle_name,student_last_name=y.student_last_name FROM
(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 1), ' ', -1) as student_first_name,
SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 2), ' ', -1) as student_middle_name,
SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 3), ' ', -1) as student_last_name
FROM   ms_student) y 

我的内部查询工作正常。但在更新中有问题

2 个答案:

答案 0 :(得分:0)

为什么需要使用简单更新命令

的子查询
update ms_student
set 
student_first_name= SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 1), ' ', -1),
student_middle_name = SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 2), ' ', -1),
student_last_name = SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 3), ' ', -1) ;

答案 1 :(得分:0)

你在update子句中有多个表

Update ms_student x,(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 1), ' ', -1) as student_first_name,
    SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 2), ' ', -1) as student_middle_name,
    SUBSTRING_INDEX(SUBSTRING_INDEX(student_name, ' ', 3), ' ', -1) as student_last_name
    FROM   ms_student) y
    set x.student_first_name=y.student_first_name,x.student_middle_name=y.student_middle_name,x.student_last_name=y.student_last_name;