【问题标题】:mySQL foreach row run an updatemySQL foreach 行运行更新
【发布时间】:2020-10-28 04:40:59
【问题描述】:
  • Progress 表有 progressIdlessonIduniqueId
  • TestResults 表有progressId
  • TestResults表 现在有lessonIduniqueId

这条 SQL 语句 foreach progrssId in TestResults 将从 Progress 得到 lessonIduniqueId

select progressId,
       (select lessonId from Progress p where p.progressId = TestResults.progressId) as lessonId,
       (select uniqueId from Progress p where p.progressId = TestResults.progressId) as uniqueId
from TestResults
where progressId is not null;

现在我想更新 lessonIduniqueId 中的TestResults 表foreach 关联progressId。我需要foreach的想法。我不知道如何在 SQL 中做到这一点。

【问题讨论】:

  • 正如重复问题的答案所示,您可以直接将选择的结果插入到表格中。但是,在这种特殊情况下,我真的不明白您为什么要这样做,因为您可以通过连接检索关联的值。
  • 您确定要在表 TestResults 中插入新行吗?也许您想使用 Progress 中的列值更新现有行?
  • 我正在尝试删除这些旧列
  • 是的,更新不插入
  • 感谢任何帮助或提示。非常感谢

标签: mysql sql


【解决方案1】:

你需要在UPDATE语句中加入2个表:

update TestResults t
inner join Progress p on p.progressId = t.progressId
set t.lessonId = p.lessonId,
    t.uniqueId = p.uniqueId 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-18
    • 2018-12-14
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 2015-11-04
    • 1970-01-01
    相关资源
    最近更新 更多