'Changing a value of a whole column retrieved by a query SQLite

This is my DB structure:

DB keys and relations

My goal is to retrieve each patient's last trial (only if their last trial is on level 0).

I used max to retrieve the last trial of each patient because trialId is increasing by 1 each trial.

That is the SQL code that does it :

SELECT 
    Patient.Id, Patient.PatientName,
    ObstacleSettings.LevelNumber, ObstacleSettings.LevelName,
    MAX(Trial.Id)
FROM 
    Session, Patient, Trial, ObstacleSettings
WHERE 
    Session.PatientId = Patient.Id 
    AND Session.Id = Trial.SessionId 
    AND Trial.FinalObstacleSettings = ObstacleSettings.Id
    AND ObstacleSettings.LevelNumber < 1
GROUP BY
    Patient.Id

Result : query

All I want to do is change columns(LevelName, LevelNumber) to be 'Level 1' and 1.

Here is what I tried to execute :

UPDATE ObstacleSettings
SET LevelName = 'Level 1*', LevelNumber = 1
WHERE ObstacleSettings.Id IN (SELECT MAX(Trial.Id)
                              FROM Session, Patient, Trial, ObstacleSettings
                              WHERE Session.PatientId = Patient.Id 
                                AND Session.Id = Trial.SessionId 
                                AND Trial.FinalObstacleSettings = ObstacleSettings.Id
                                AND ObstacleSettings.LevelNumber < 1
                              GROUP by Patient.Id) 

The modification I need happens to the Obstacle setting table but not to the correct rows, what happens is ObstacleSetting rows 1091, 1094 get changed (because it uses the max trial ID instead of correspondent ObstacleSetting Id row).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source