- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy data from one field to another on every row in MySQL?
To copy data from one field to another on every row, use the UPDATE command.
Let us first create a table −
mysql> create table DemoTable ( StudentId int, StudentFirstName varchar(20), StudentMarks int default 0 ); Query OK, 0 rows affected (0.49 sec)
Following is the query to insert records in the table using insert command −
mysql> insert into DemoTable(StudentId,StudentFirstName) values(89,'Larry'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentId,StudentFirstName) values(35,'Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(StudentId,StudentFirstName) values(48,'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentId,StudentFirstName) values(78,'David'); Query OK, 1 row affected (0.61 sec)
Following is the query to display records from the table using select command −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+--------------+ | StudentId | StudentFirstName | StudentMarks | +-----------+------------------+--------------+ | 89 | Larry | 0 | | 35 | Robert | 0 | | 48 | Chris | 0 | | 78 | David | 0 | +-----------+------------------+--------------+ 4 rows in set (0.00 sec)
Following is the query to copy data from one field to another on every row. Here, we are copying all the values of StudentId to StudentMarks −
mysql> update DemoTable set StudentMarks=StudentId; Query OK, 4 rows affected (0.34 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us display all records from the table to check all rows have been updated or not −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+--------------+ | StudentId | StudentFirstName | StudentMarks | +-----------+------------------+--------------+ | 89 | Larry | 89 | | 35 | Robert | 35 | | 48 | Chris | 48 | | 78 | David | 78 | +-----------+------------------+--------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to copy rows from one table to another in MySQL?
- Simplest way to copy data from one table to another new table in MySQL?
- How to copy a table from one MySQL database to another?
- MySQL statement to copy data from one table and insert into another table
- How to copy tables or databases from one MySQL server to another MySQL server?
- Copy from one column to another (different tables same database) in MySQL?
- Insert data from one table to another in MySQL?
- Insert data from one schema to another in MySQL?
- MySQL query to copy records from one table to another with different columns
- How to copy a collection from one database to another in MongoDB?
- How to copy files from one folder to another using Python?
- How to copy files from one server to another using Python?
- Copy values from one array to another in Numpy
- How to derive value of a field from another field in MySQL?
- How to copy certain files from one folder to another using Python?

Advertisements