1

我尝试运行以下命令:

UPDATE project JOIN folder ON folder.project_id = project.id
  SET project.source_folder_id = folder.id
  WHERE folder.name = 'source';

当然,我得到了著名的错误 1175: You are using safe update mode and you try to update a table without a WHERE that uses a KEY column。当然,我解决了这个问题并写道:

 … WHERE id > 0 AND folder.name = 'source';

这当然会失败,因为id它是模棱两可的。所以我写了

  … project.id > 0 AND folder.name = 'source';

我仍然得到那个错误。我尝试了以下每种变体,但均未成功:

  … WHERE folder.id > 0 AND folder.name = 'source';
  … WHERE project.id > 0 AND folder.id > 0 AND folder.name = 'source';
  … WHERE (project.id > 0 AND folder.id > 0 AND folder.name = 'source');
  … WHERE (project.id, folder.id) > (0,0) AND folder.name = 'source';

我仔细检查了两个表,id列实际上是主键,情况就是这样。我知道我可以SET SQL_SAFE_UPDATES=0;,但这感觉更像是一种黑客行为,可能会被团队拒绝,我想了解为什么查询不起作用?是否可以将其修复为在安全模式下运行,如果可以,如何解决?

version:                 5.7.19-log
innodb_version:          5.7.19
version_comment:         MySQL Community Server (GPL)
version_compile_machine: x86_64
version_compile_os:      Win64
4

1 回答 1

0

尝试这个

UPDATE project p 
SET p.source_folder_id = (Select f.id From folder f 
                          where f.project_id = p.id and f.name = 'source');
于 2018-07-30T10:28:53.330 回答