- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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 Articles
- 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?
- Best way to update a single column in a MySQL table?
- How to copy a table from one MySQL database to another?
- How to copy rows from one table to another in MySQL?
- What is the easiest way to store date in MySQL database?
- Adding new enum column to an existing MySQL table?
- MySQL query to count number of duplicate values in a table column
- Copy all rows of a table to another table in MySQL?
- Effective way to add integers based on table values in MySQL?
- MySQL query to increment one of the column values
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?

Advertisements