Spiga

SQL Query : Copy one Column to another column in MySQL

by Gabi Solomon

I wanted to do add a new column to a table and to copy the values from that a diferent to column ( from the same table ) to the new column. And even though i thought of very integrates query’s at first, the solution was quite simple :D ( as always ).

[SQL]
ALTER TABLE `table_name` ADD `new_column` TEXT NOT NULL;
UPDATE `table_name` SET `new_column` = `old_column`;
[/SQL]

Nice and simple.
Cheers

  • http://askbahar.com Habib Ullah Bahar

    very handy

  • http://www.cutmyhair.com.au/ hairdresser

    Brilliant… so simple! I just timed out 5 times trying to export the data to them alter the SQL to re-import… I gave up and google “copy” and found this!

    Thanks

  • VENKATESH_V_S

    Very Smart !!

  • http://TedAvery.ca Ted Avery

    Thank you, kind sir!

  • Thedigitalblade

    This only copies the first row of the source column… it's completely useless!!!

  • http://twitter.com/legendri Robertino Darmati

    Nice one
    @Thedigitalblade you must be doing something wrong

  • User

    Works great thanks and leave the old column untouched!

  • Rajeev Bu

    Thanks a lot.

  • http://www.modularhomesnetwork.com Modular Home

    I would like to thank you for sharing your thoughts.

  • http://www.findprefab.com Prefab Home

    This site is a must visit for everyone.Great job!

  • http://twitter.com/_maars Andrey Nemchenko

    Thanks!

  • Vivek Kapoor

    This was pretty simple and fast. Much thanks!

  • http://www.findprefab.com Manufactured House

    Wow ! This is best blog i have read on the topic.

  • http://www.findprefab.com/ Prefab Home

    It’s good to have somebody else who’s done it confirm how good it works. You have incredible results, by the way.

  • Balakarthikeyan07

    Nice thank u

  • http://www.modularhomesnetwork.com/ Modular Homes

    I do intend to change Loan officers.

  • http://www.modularhomesnetwork.com/ Manufactured Homes

    Very insightful and interesting. I hope some of the execs read and heed your suggestions.

  • Anonymous

    Great site and I look forward to checking out your updates!
    Love this!

  • http://www.modularhomesnetwork.com/ Modular Homes

    Great read! I wish you could follow up on this topic..

  • http://www.modularhomesnetwork.com/ Modular Homes

    Great read! I wish you could follow up on this topic..

  • Rob

    awesome!

    I was sweating on changing the contents of a table in joomla and this little query saved the day :)

  • Gopal, Mumbai

    Really a nice and easy Query. Thank you Dear. 

  • Deepak Jain

    Great……:)
    Thanks

  • Deepak Jain

    Great……:)
    Thanks

  • C0d3M0nk3y

    Depending on your version of mysql you might have to change that a little.  I had to use:
    UPDATE `tablename` SET `new_column` = `old_column`;

    I’m not sure why, or what version but if you are having issues, try it this way also.

  • Farhanali89

    Thx bro……

  • User

    Fabulous!!!! Worked perfect!

    Saved me lots of time

  • http://vouch2go.com/ Yaniv

    Perfect!
    Thanks :)

  • Jayapalagowda

    Nice ……..

  • Mihai

    It works! Thanks.