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,055 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
CoyotesIT earned 250 total points
ID: 21875835
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
ID: 21875882
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
ID: 21875912
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 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