I have the following query:
UPDATE lessonstatus INNER JOIN user ON lessonstatus.user_id = user.user_id SET user_id = (SELECT user_id FROM user WHERE username = 'too_many_accounts') WHERE last_name = 'stupid' AND first_name = 'user' AND username != 'too_many_accounts' AND lessonstatus.lesson_id NOT IN (SELECT lesson_id FROM lessonstatus WHERE user_id = 1);
However, I get the following error when trying to execute it:
Error Code : 1093 You can't specify target table 'lessonstatus_rtab' for update in FROM clause
How would I fix this query so that it works?
You can’t
SELECTfrom a table (even in a subquery) that you’re updating in the same query. That’s what the error ‘can’t specify target table’ means.But you can join
userandlessonstatusmultiple times in theUPDATEstatement, and use the join criteria creatively to pick out the individual row you want.The way to simulate
NOT INwith a join is to do aLEFT OUTER JOIN. Where the right side of that join is not matched, that’s whereNOT INwould be true.nb: I have tested this query for syntax, but not with real data. Anyway, it should get you started.