sql - Swapping MySQL Data between Rows -
the goal run mysql query swaps around table id's.
the table
id tableid car 1 1 ford mustang 2 1 ford focus 3 1 ford ranger 4 2 toyota 4runner 5 2 toyota celica 6 3 chevy camaro 7 4 cadillac escalade 8 4 cadillac cts 9 6 dodge charger 10 6 dodge ram 11 6 dodge caravan
if run these queries
update table set tableid='2' tableid='1' update table set tableid='1' tableid='2' update table set tableid='5' tableid='6'
so idea i'd swap tableid's 1 , 2. however, happen after first query of tableid 1 merge tableid 2. second query update of tableid 2 (which includes old tableid 1 , tableid 2) tableid 1.
the last query have no issues there no conflict. however, how write query swap 2 tableid's in instance without tables getting messed (joining)?
just in 1 query, using case
statement:
update table set tableid = (case when tableid = '1' '2' when tableid = '2' '1' when tableid = '5' '6' end) tableid in ('1', '2', '5');
Comments
Post a Comment