Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy data from one field to another on every row

Tags:

sql

mysql

So I've got:

id    number
 1      0
 2      0
 3      0

Is there a sql statement to copy everything from id into number?

I'm about to write a php scrip to select, then update every row. My SQL knowledge is pretty basic, but I'm sure there's a smart guy way to to do this:

Background: The id used to be a number that was displayed in the app and was unique. That number is no longer unique with some new features I'm adding--so I need to move it to a field that isn't unique while maintaining the integrity of the old.

like image 273
Ryan Florence Avatar asked Jun 02 '09 22:06

Ryan Florence


People also ask

How can I copy data from one column to another in the different table?

Click the tab for the table with the columns you want to copy and select those columns. From the Edit menu, click Copy. Click the tab for the table into which you want to copy the columns. Select the column you want to follow the inserted columns and, from the Edit menu, click Paste.

How do I insert values from one column to another column in SQL?

INSERT INTO Syntax Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)

How can we update one column value from another column in the same table?

In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.

How do I extract data from one table to another in SQL?

The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.


1 Answers

You can use an update statement and reference the columns. Just do the following:

update mytable set number = id

That sets number equal to id on each row. Enjoy!

like image 166
Eric Avatar answered Oct 14 '22 19:10

Eric