What I’m looking to do is insert a record, then deactivate previous records with the same ID because they will no longer be in use. However, I’m looking to do this in the simplest way possible. Deleting the record really isn’t an option.
Attempted order of operations:
- Insert with active inUse value
inUse = 1 - Update the following records for the same ID that are no longer in use:
inUse = 0
My first thought was to run this query:
UPDATE page_tags
SET inUse = IF(inUse = 1, 0, 1)
WHERE page_id = 23678459
AND tag_id NOT IN (10, 4);
The only problem with this query is that if it’s run again, it will toggle all of those deactivated values back to 1. I need all of the tags for the specific ID to only toggle back if they are being targeted by the WHERE statement.
@John P has a decent answer, however his answer requires the use of triggers. That to me seems to be more than needed to solve the problem at hand. The current working solution is:
Create an Unique Index on
page_tags.page_idandpage_tags.tag_id.Update all rows where the *page_id = 234234*:
Insert tags: