无法从下一个记录错误1093更新具有相同列的表

cygmwpex  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(202)

这个问题在这里已经有答案了

mysql错误1093-无法在from子句中指定更新的目标表(16个答案)
两年前关门了。
我需要使用下一个记录的数据(根据incremental gkey字段)更新一个日期错误(1970-01-01)的表的记录。
所以,如果我做这个查询:

SELECT aa.gkey,
       aa.course_date,
       (select course_date from BI.fact_training_event_tbl bb where bb.gkey = (
            select min(cc.gkey) 
            from BI.fact_training_event_tbl cc 
                 where cc.gkey > aa.gkey)) as next_date
from BI.fact_training_event_tbl aa
where course_date = '1970-01-01'

它正确地显示了记录,如预期的那样:

gkey   course_date  next_date
====   ===========  =========
4103   1970-01-01   2017-03-23
4884   1970-01-01   2017-03-22
5047   1970-01-01   2017-03-23

我现在需要用下一个日期更新course\u date字段,但是如果我尝试运行以下命令,就会得到
错误代码1093。不能在from子句中为update指定目标表“aa”:

update BI.fact_training_event_tbl aa
    set course_date =
    (select course_date from BI.fact_training_event_tbl bb where bb.gkey = (
            select min(cc.gkey)
    from BI.fact_training_event_tbl cc
         where cc.gkey > aa.gkey))
where course_date = '1970-01-01'

有什么办法让我成功吗?
我想澄清的是,这个问题之前没有得到回答,因为关于错误1093的线程显示了一个简单的子查询。在我的例子中,我正在查找引用主表的下一条记录。请不要把它标为复制品。

jfewjypa

jfewjypa1#

您可以尝试使用表名而不是别名,因为内部子查询不知道别名 aa ```
update BI.fact_training_event_tbl
set course_date =
(
select course_date
from BI.fact_training_event_tbl bb
where bb.gkey =
(
select min(cc.gkey)
from BI.fact_training_event_tbl cc
where cc.gkey > BI.fact_training_event_tbl.gkey
)
)
where course_date = '1970-01-01'

相关问题