Solved

need mysql command to copy/update one column with another column's data in the same row.

Posted on 2008-06-26
3
4,046 Views
Last Modified: 2009-12-19
This is probably pretty simple to do.  But, I need a mysql command to copy/update one empty column with another column's data in the same row for multiple rows in a table.

I need column1's data to copy over to column2 for multiple rows all in the same table ie.
Row1 column1 (some value)  column2 (no value)
Row2 column1 (some value)  column2 (no value)
Row3 column1 (some value)  column2 (no value)

to
Row1 column1 (some value)  column2 (some value)
Row2 column1 (some value)  column2 (some value)
Row3 column1 (some value)  column2 (some value)
0
Comment
Question by:allwebnow
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
CoyotesIT earned 250 total points
Comment Utility
Just use

update <your_table> set column2 = column1

That will place the values in column 1 into column 2

Good luck!
0
 
LVL 10

Expert Comment

by:bluefezteam
Comment Utility
this will give you a start, not sure how to loop through in mySQL though

update tablename set column2 = (select column1 from tablename where column1 = row1) where column1 = row1

this will update the first row you need something like this though (pseudo code)

for i = 1 to [total number of records]
update tablename set column2 = (select column1 from tablename where column1 = i) where column1 = i
loop



0
 
LVL 8

Expert Comment

by:CoyotesIT
Comment Utility
There is no need to loop through the table, setting the value column2 = column1 references the row that is currently being processed by your select statement. Although bluefezteam's method would work, it is unneccessary and more intense on your database.

UPDATE <TABLE> SET Column2 = Column1

is the method that will give you what you need without wasting resources

~CoyotesIT
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now