Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:41:42+00:00 2026-05-11T06:41:42+00:00

I have a MySQL table with coordinates; the column names are X and Y.

  • 0

I have a MySQL table with coordinates; the column names are X and Y. Now I want to swap the column values in this table, so that X becomes Y and Y becomes X. The most apparent solution would be renaming the columns, but I don’t want to make structure changes since I don’t necessarily have permissions to do that.

Is this possible to do with UPDATE in some way? UPDATE table SET X=Y, Y=X obviously won’t do what I want.


Please note that my restriction on permissions, mentioned above, effectively prevents the use of ALTER TABLE or other commands that change the table/database structure. Renaming columns or adding new ones are unfortunately not options.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:41:42+00:00Added an answer on May 11, 2026 at 6:41 am

    I just had to deal with the same and I’ll summarize my findings.

    1. The UPDATE table SET X=Y, Y=X approach obviously doesn’t work, as it’ll just set both values to Y.

    2. Here’s a method that uses a temporary variable. Thanks to Antony from the comments of http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/ for the ‘IS NOT NULL’ tweak. Without it, the query works unpredictably. See the table schema at the end of the post. This method doesn’t swap the values if one of them is NULL. Use method #3 that doesn’t have this limitation.

      UPDATE swap_test SET x=y, y=@temp WHERE (@temp:=x) IS NOT NULL;

    3. This method was offered by Dipin in, yet again, the comments of http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/. I think it’s the most elegant and clean solution. It works with both NULL and non-NULL values.

      UPDATE swap_test SET x=(@temp:=x), x = y, y = @temp;

    4. Another approach I came up with that seems to work:

      UPDATE swap_test s1, swap_test s2 SET s1.x=s1.y, s1.y=s2.x WHERE s1.id=s2.id;

    Essentially, the 1st table is the one getting updated and the 2nd one is used to pull the old data from.
    Note that this approach requires a primary key to be present.

    This is my test schema:

    CREATE TABLE `swap_test` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `x` varchar(255) DEFAULT NULL,   `y` varchar(255) DEFAULT NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB;  INSERT INTO `swap_test` VALUES ('1', 'a', '10'); INSERT INTO `swap_test` VALUES ('2', NULL, '20'); INSERT INTO `swap_test` VALUES ('3', 'c', NULL); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 79k
  • Answers 79k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, INamingContainer defines the context where the controls need to… May 11, 2026 at 4:05 pm
  • Editorial Team
    Editorial Team added an answer [<System.Runtime.CompilerServices.Extension>] module Methods = [<System.Runtime.CompilerServices.Extension>] let Exists(opt : string option)… May 11, 2026 at 4:05 pm
  • Editorial Team
    Editorial Team added an answer there is yet another possibility, with slightly nicer scrolling simulation:… May 11, 2026 at 4:05 pm

Related Questions

I have a MySQL table with approximately 3000 rows per user. One of the
I have a MySQL table with utf8 general ci collation. In the table, I
I have a MySQL table with an auto increment primary key. I deleted some
I have a MySQL table with multiple fields, and I'd like to find any

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.