

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Easiest way to copy values of one column to a new table in MySQL?
For this, use AS select statement. Let us first create a table −
mysql> create table DemoTable1 ( Score int ); Query OK, 0 rows affected (1.22 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(89); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1 values(98); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1 values(91); Query OK, 1 row affected (0.24 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+-------+ | Score | +-------+ | 89 | | 98 | | 91 | +-------+ 3 rows in set (0.00 sec)
Following is the query to copy values of one column to a new table −
mysql> create table DemoTable2( StudentScore int )AS select Score AS StudentScore from DemoTable1; Query OK, 3 rows affected (0.81 sec) Records: 3 Duplicates: 0 Warnings: 0
Let us check the table records once again −
mysql> select StudentScore from DemoTable2;
This will produce the following output −
+--------------+ | StudentScore | +--------------+ | 89 | | 98 | | 91 | +--------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- Simplest way to copy data from one table to another new table in MySQL?
- Easiest way to get number of rows in a MySQL table?
- Copy column values from one table into another matching IDs in MySQL
- Copy values of one column to another using INSERT and SELECT in a single MySQL query
- The easiest way to insert date records in MySQL?
- How to copy a table from one MySQL database to another?
- Best way to update a single column in a MySQL table?
- How to copy rows from one table to another in MySQL?
- MySQL query to increment one of the column values
- Copy all rows of a table to another table in MySQL?
- What is the easiest way to store date in MySQL database?
- MySQL command to copy table?
- MySQL statement to copy data from one table and insert into another table
- Easiest way to sort an array in MongoDB
- Adding new enum column to an existing MySQL table?
Advertisements