Life

Friday, November 1, 2019

Table 'name' is specified twice, both as a target for 'UPDATE' and as a separate source for data

明明一个在ORACLE里平平无奇的UPDATE语句放到较旧的MariaDB(10.0.38)里面变得如此的麻烦~
还用上次的表好了,原始数据如下:
现在我想把第一条的course后面加一个’类‘字,我们可能会这样写:

update student t set 
course = (concat((select  course from student t where t.sno=8),'类')) 
where t.sno=8;
当然它在ORALCE中是没有问题的,可是放到MariaDB中运行它,就会返回“Table 't' is specified twice, both as a target for 'UPDATE' and as a separate source for data”的错误提示。
大概就是说你不能自己又当“因”,又当“果”,(额,自己打自己?)
那既然不允许被更新的表直接出现在子查询中,那我们可以把它放到缓存表里,就像这样:

update student t set course = 
(concat((select * from(select  course from student t where t.sno=1)as temp),'类')) 
where t.sno=1;
执行这条语句就可以回避之前的尴尬错误。效果如下:
事实上,在MariaDB的官方文档中对这样的UPDATE情况有说明:
文档说的很明白了,对于这种源和目标是同一张表单情况,截至到10.3.1版本都是不支持的,即需要用我上述的方法“曲线救国”,自10.3.2版本后这样的写法就是可以的了~
版权声明
本博客所有的原创文章,作者皆保留版权。转载必须包含本声明,保持本文完整,并以超链接形式注明作者Leslie Tien和本文原始地址:
https://leslietien.blogspot.com/2019/11/table-name-is-specified-twice-both-as.html

No comments:

Post a Comment