40

我正在尝试使用 UPDATE 更新表中一堆行中的列值。问题是我需要使用子查询来导出该列的值,并且它依赖于同一张表。这是查询:

UPDATE user_account student
SET student.student_education_facility_id = (
   SELECT teacher.education_facility_id
   FROM user_account teacher
   WHERE teacher.user_account_id = student.teacher_id AND teacher.user_type = 'ROLE_TEACHER'
)
WHERE student.user_type = 'ROLE_STUDENT';

通常如果教师和学生在 2 个不同的表中,mysql 不会抱怨。但由于它们都使用同一个表,mysql 会喷出这个错误:

ERROR 1093 (HY000): 您不能在 FROM 子句中指定目标表 'student' 进行更新

有什么办法可以强制mysql进行更新吗?我 100% 肯定 from 子句不会随着行的更新而受到影响。

如果没有,是否有另一种方法可以编写此更新 sql 以达到相同的效果?

谢谢!

编辑:我想我让它工作了:

UPDATE user_account student
LEFT JOIN user_account teacher ON teacher.user_account_id = student.teacher_id
SET student.student_education_facility_id = teacher.education_facility_id
WHERE student.user_type = 'ROLE_STUDENT';
4

7 回答 7

44

为您提供一些参考http://dev.mysql.com/doc/refman/5.0/en/update.html

UPDATE user_account student 
INNER JOIN user_account teacher ON
   teacher.user_account_id = student.teacher_id 
   AND teacher.user_type = 'ROLE_TEACHER'
SET student.student_education_facility_id = teacher.education_facility_id
于 2010-11-24T15:24:50.517 回答
20

具有更清晰的表名和列名的抽象示例:

UPDATE tableName t1
INNER JOIN tableName t2 ON t2.ref_column = t1.ref_column
SET t1.column_to_update = t2.column_desired_value

正如@Nico 所建议的那样

希望这对某人有所帮助。

于 2014-05-21T01:21:42.720 回答
5
UPDATE user_account 
SET (student_education_facility_id) = ( 
    SELECT teacher.education_facility_id
    FROM user_account teacher
    WHERE teacher.user_account_id = teacher_id
    AND teacher.user_type = 'ROLE_TEACHER'
)
WHERE user_type = 'ROLE_STUDENT'

以上是示例更新查询...

您可以使用更新 SQL 语句编写子查询,您不需要为该表提供别名。为子查询表提供别名。我试过了,它对我来说很好用......

于 2013-11-20T14:28:59.103 回答
2
UPDATE user_account student

SET (student.student_education_facility_id) = (

   SELECT teacher.education_facility_id

   FROM user_account teacher

   WHERE teacher.user_account_id = student.teacher_id AND teacher.user_type = 'ROLE_TEACHER'

)

WHERE student.user_type = 'ROLE_STUDENT';
于 2013-06-07T15:54:50.253 回答
2

我需要这个用于 SQL Server。这里是:

UPDATE user_account 
SET student_education_facility_id = cnt.education_facility_id
from  (
   SELECT user_account_id,education_facility_id
   FROM user_account 
   WHERE user_type = 'ROLE_TEACHER'
) as cnt
WHERE user_account.user_type = 'ROLE_STUDENT' and cnt.user_account_id = user_account.teacher_id

我认为它适用于其他 RDBMS(请确认)。我喜欢这种语法,因为它是可扩展的。

我需要的格式实际上是这样的:

UPDATE table1 
SET f1 = cnt.computed_column
from  (
   SELECT id,computed_column --can be any complex subquery
   FROM table1
) as cnt
WHERE cnt.id = table1.id
于 2015-02-23T17:26:25.500 回答
0

我需要在子查询中进行求和,但没有一个解决方案有效。所以我在主表上创建了一个视图,并且可以从中访问同一个表。

Create user_account_VW as select * from user_account

UPDATE user_account
SET user_account.student_education_facility_id =
    (SELECT user_account_VW.education_facility_id
     FROM user_account_VW
     WHERE user_account_VW.user_account_id = user_account.teacher_id
         AND user_account_VW.user_type = 'ROLE_TEACHER')
于 2021-05-27T08:03:57.513 回答
-3
UPDATE user_account student, (
   SELECT teacher.education_facility_id as teacherid
   FROM user_account teacher
   WHERE teacher.user_account_id = student.teacher_id AND teacher.user_type = 'ROLE_TEACHER'
) teach SET student.student_education_facility_id= teach.teacherid WHERE student.user_type = 'ROLE_STUDENT';
于 2012-06-07T11:56:01.590 回答