简体   繁体   English

SQL UPDATE与子查询引用MySQL中的同一个表

[英]SQL UPDATE with sub-query that references the same table in MySQL

I'm trying to update a column's value in a bunch of rows in a table using UPDATE. 我正在尝试使用UPDATE更新表中一堆行中的列值。 The problem is that I need to use a sub-query to derive the value for this column, and it depends on the same table. 问题是我需要使用子查询来派生此列的值,它依赖于同一个表。 Here's the query: 这是查询:

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';

Ordinarily if teacher and student were in 2 different tables, mysql wouldn't complain. 通常,如果老师和学生在两个不同的表中,mysql不会抱怨。 But since they are both using the same table, mysql spews out this error instead: 但由于他们都使用相同的表,mysql反而吐出了这个错误:

ERROR 1093 (HY000): You can't specify target table 'student' for update in FROM clause 错误1093(HY000):您无法在FROM子句中为更新指定目标表'student'

Is there any way I can force mysql to do the update? 有什么办法可以强制mysql进行更新吗? I am 100% positive the from clause will not be affected as the rows are updated. 我100%肯定,因为更新行,所以from子句不会受到影响。

If not, is there another way I can write this update sql to achieve the same affect? 如果没有,是否有另一种方法可以编写此更新sql来实现同样的效果?

Thanks! 谢谢!

EDIT: I think I got it to work: 编辑:我想我得到了它的工作:

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';

Some reference for you http://dev.mysql.com/doc/refman/5.0/en/update.html 一些参考资料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

Abstract example with clearer table and column names: 具有更清晰的表和列名称的抽象示例:

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

As suggested by @Nico 正如@Nico所建议的那样

Hope this help someone. 希望这能有所帮助。

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'

Above are the sample update query... 以上是示例更新查询...

You can write sub query with update SQL statement, you don't need to give alias name for that table. 您可以使用更新SQL语句编写子查询,您不需要为该表提供别名。 give alias name to sub query table. 为别查询表提供别名。 I tried and it's working fine for me.... 我试过,它对我来说很好......

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';

I needed this for SQL Server. 我需要这个用于SQL Server。 Here it is: 这里是:

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

I think it works with other RDBMSes (please confirm). 我认为它适用于其他RDBMS(请确认)。 I like the syntax because it's extensible. 我喜欢语法,因为它是可扩展的。

The format I needed was this actually: 我需要的格式实际上是:

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
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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM