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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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