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
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,052 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
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

791 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