Node.JS
Node.JS

Reputation: 1570

mySQL foreach row run an update

This SQL statement foreach progrssId in TestResults will get lessonId and uniqueId from Progress:

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;

Now I want to update lessonId and uniqueId in TestResults table foreach associated progressId. I need foreach idea. I don't know how to do that in SQL.

Upvotes: 0

Views: 468

Answers (1)

forpas
forpas

Reputation: 164069

You need to join the 2 tables in the UPDATE statement:

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

Upvotes: 1

Related Questions