我有一个表,其中包含大约 100 条记录作为模板和超过 10000 条复制记录回溯到原始记录。我需要从相应的原始副本中重新初始化副本中的一个字段,但无法弄清楚为什么我不能从子查询中引用外部字段。
示例表(SQLFiddle 链接):
ID OriginalID String
----------------------------
1 NULL original 1
2 NULL original 2
3 1 copy of 1
4 2 copy of 2
5 2 another copy of 2
期望的结果:
1 no change
2 no change
3 string should change to 'original 1'
4 string should change to 'original 2'
5 string should change to 'original 2'
我试过的 SQL:
update data as target set target.string=(select string from
(select string from data as source where source.id=target.originalid) as x);
错误:
Unknown column 'target.originalid' in 'where clause'