sql - Mysql: Toggle value of int field under two conditions for UPDATE query -


what i'm looking insert record, deactivate previous records same id because no longer in use. however, i'm looking in simplest way possible. deleting record isn't option.

attempted order of operations:

  1. insert active inuse value inuse = 1
  2. update following records same id no longer in use: inuse = 0

my first thought run query:

update page_tags set inuse = if(inuse = 1, 0, 1) page_id = 23678459 , tag_id not in (10, 4);

the problem query if it's run again, toggle of deactivated values 1. need of tags specific id toggle if being targeted statement.

sounds job trigger. perhaps (pseudocode)?

update handling reuse of previuos tags:

do insert/update:

insert ... on duplicate key update 

then use 2 triggers, 1 inserts , 1 updates.

create trigger tr_inuse_insert before insert on page_tags   each row begin     update page_tags set inuse=0 page_id = new.page_id;   end;  create trigger tr_inuse_update before update on page_tags   each row begin     update page_tags set inuse=0 page_id = new.page_id;   end; 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -