使用引用 MySQL 中同一表的子查询的 SQL UPDATE
我正在尝试使用 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';
通常,如果教师和学生位于两个不同的表中,mysql 不会抱怨。但由于它们都使用同一个表,mysql反而会输出这个错误:
ERROR 1093 (HY000): You can't指定目标表'student'在FROM子句中进行更新
有什么办法吗?可以强制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';
I'm trying to update a column's value in a bunch of rows in a table using 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. But since they are both using the same table, mysql spews out this error instead:
ERROR 1093 (HY000): You can't specify target table 'student' for update in FROM clause
Is there any way I can force mysql to do the update? I am 100% positive the from clause will not be affected as the rows are updated.
If not, is there another way I can write this update sql to achieve the same affect?
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';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
给你一些参考 http://dev.mysql.com/doc/refman/5.0/en/更新.html
Some reference for you http://dev.mysql.com/doc/refman/5.0/en/update.html
具有更清晰的表和列名称的抽象示例:
正如@Nico 希望
这对某人有帮助。
Abstract example with clearer table and column names:
As suggested by @Nico
Hope this help someone.
上面是示例更新查询...
您可以使用更新 SQL 语句编写子查询,不需要为该表提供别名。为子查询表提供别名。我尝试过,它对我来说效果很好......
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. give alias name to sub query table. I tried and it's working fine for me....
我需要这个用于 SQL Server。就是这样:
我认为它可以与其他 RDBMS 一起使用(请确认)。我喜欢这种语法,因为它是可扩展的。
我需要的格式实际上是这样的:
I needed this for SQL Server. Here it is:
I think it works with other RDBMSes (please confirm). I like the syntax because it's extensible.
The format I needed was this actually:
我需要在子查询中求和,但没有一个解决方案有效。
因此,我在主表上创建了一个视图,并且可以从中访问同一个表。
创建 user_account_VW 作为 select * from user_account
I needed to do a sum in the sub query and none of the solution was working.
So I created a view on the main table and could access the same table from it.
Create user_account_VW as select * from user_account