Swapping column content

mrfdes's profile image mrfdes posted 4 weeks ago in General Permalink

Hi, I use HeidiSQL 12.12.0.7122 on Windows 11 64-bit. Lately something strange has been happening, not sure why,might also be an inattention on my part. The problem is that, on my MySQL database, conssting of 3 columns, the contents of the last 2 columns have swapped. This has heppened for the last 100 entries or so.

My question is: is there a "simple" way to swap the contents of the wrong colums on order to correct them without having to do it manully column by column (which would obviously be very time consuming)?

Thank you in advance.

ansgar's profile image ansgar posted 4 weeks ago Permalink

You mean the data of two columns in 100 rows have swapped their contents?

If it's ordinary text or numbers, you could create a temporary new column and use that to re-swap:

UPDATE mytable SET tempcol=col1 WHERE id BETWEEN x and y;
UPDATE mytable SET col1=col2 WHERE id BETWEEN x and y;
UPDATE mytable SET col2=tempcol WHERE id BETWEEN x and y;
mrfdes's profile image mrfdes posted 4 weeks ago Permalink

OK, ansgar. Thank you so much for your reply. That is a lovely solution. I am very much obliged.

Please login to leave a reply, or register at first.