This question has been asked a few different ways on this site, but I can't seem to figure how to implement an update in this specific case. Generally, I am trying to update the most recent ContactNote for every Contact that belongs to a TeamId and has specific Categories. It's a fairly straight forward DB setup (see diagram below).
I have successfully created a "select clause" that returns all the records I would like to update, but when I add the UPDATE language, MySQL gives me the error: Error Code: 1093. You can't specify target table 'cn1' for update in FROM clause
Any guidance is appreciated.
Working SELECT Clause
SELECT cn1.* from ContactNote cn1
INNER JOIN Contact contact on contact.ContactId = cn1.ContactId
INNER JOIN ContactCategory contactCategory on contactCategory.ContactId = contact.ContactId
INNER JOIN Category category on category.CategoryId = contactCategory.CategoryId
INNER JOIN ContactNote contactNote
ON contactNote.ContactNoteId =
(SELECT cn2.ContactNoteId
FROM ContactNote cn2
WHERE contact.ContactId = cn2.ContactId
ORDER BY cn2.NoteDateTime DESC
LIMIT 1
)
where contact.TeamId = 1
and contact.SpouseLastName = 'Rhodes'
and category.`Name` in ('Sphere')
;
Non-Working UPDATE Clause
update ContactNote cn1
INNER JOIN Contact contact on contact.ContactId = cn1.ContactId
INNER JOIN ContactCategory contactCategory on contactCategory.ContactId = contact.ContactId
INNER JOIN Category category on category.CategoryId = contactCategory.CategoryId
INNER JOIN ContactNote contactNote
ON contactNote.ContactNoteId =
(SELECT cn2.ContactNoteId
FROM ContactNote cn2
WHERE contact.ContactId = cn2.ContactId
ORDER BY cn2.NoteDateTime DESC
LIMIT 1
)
SET cn1.IsProspecting = b'1'
where contact.TeamId = 1
and contact.SpouseLastName = 'Rhodes'
and category.`Name` in ('Sphere')
;
