首先,由于关于错误 1093 的线程显示了一个简单的子查询,因此之前没有回答这个问题。就我而言,我正在查找引用主表的下一条记录。请不要在没有先阅读整个问题的情况下将其标记为重复。
我需要使用下一条记录的数据(根据 gkey 字段,它是连续的 int 主键)更新日期错误(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
我现在需要用 next_date 更新 course_date 字段,但是如果我尝试运行以下命令:
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 > BI.fact_training_event_tbl.gkey))
where course_date = '1970-01-01'
我得到错误:
错误代码 1093。您无法在 FROM 子句中指定目标表 'BI.fact_training_event_tbl' 进行更新
我尝试执行此处推荐的操作:MySQL 错误 1093 - 无法在 FROM 子句中指定要更新的目标表,将查询嵌套在另一个中:
update BI.fact_training_event_tbl as zz
set course_date =
(select course_date from
(select course_date from BI.fact_training_event_tbl as bb where bb.gkey = (
select min(cc.gkey)
from BI.fact_training_event_tbl as cc
where cc.gkey > gkey)) as aa )
where course_date = '1970-01-01'
但我得到的只是将 date_course 设置为 null,而不是 next_date。
如果我尝试像这样引用主表:
where cc.gkey > BI.fact_training_event_tbl.gkey
或者
where cc.gkey > zz.gkey
它说:未知列 BI.fact_training_event_tbl.gkey 或 zz.gkey。
关于如何解决这个问题的任何想法?