我正在处理一个包含 1000 多行的表,其中两列中的数据已损坏 ( table_corrupted)。幸运的是,我有该表的过时备份,其中这两列完好无损(table_outdated)。所以我想:为什么不只替换这两列中的值并保留其余部分不变?
假设table_corrupted两者table_outdated都有 5 列:
id(整数)、name(文本)、lat(双)、lon(双)、comment(文本)
insert into `table_corrupted` (`lat`,`lon`)
select `lat`,`lon` from `table_outdated`
WHERE `table_corrupted`.`id` = `table_outdated`.`id`;
Run Code Online (Sandbox Code Playgroud)
...导致此错误:“Where 子句中的未知列 'table_corrupted.id'”
经过一些研究,我发现这是因为 SQL 是从右到左向后计算的。老实说,我没有找到解决方案 - 有什么建议吗?我究竟做错了什么?
您可以通过执行以下查询更好地连接表并简单地更新损坏表中的值
update `table_corrupted`
inner join `table_outdated` on `table_corrupted`.`id` = `table_outdated`.`id`
set `table_corrupted`.`lat`= `table_outdated`.`lat`,
`table_corrupted`.`lon`= `table_outdated`.`lon`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4256 次 |
| 最近记录: |