
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Copy a few columns from a table to another in MySQL
Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(10,'John'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1 values(11,'Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1 values(12,'Robert'); Query OK, 1 row affected (0.32 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------+--------+ | Id | Name | +------+--------+ | 10 | John | | 11 | Chris | | 12 | Robert | +------+--------+ 3 rows in set (0.00 sec)
Following is the query to copy few columns from a DemoTable1 to another table DemoTable2 −
mysql> create table DemoTable2 ( -> select Name from DemoTable1 where Id=11 -> ); Query OK, 1 row affected (0.70 sec) Records: 1 Duplicates: 0 Warnings: 0
Now check the table records of table ‘DemoTable2’ −
mysql> select *from DemoTable2;
Output
This will produce the following output −
+-------+ | Name | +-------+ | Chris | +-------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to copy records from one table to another with different columns
- How to copy a table from one MySQL database to another?
- Copy all rows of a table to another table in MySQL?
- 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?
- MySQL statement to copy data from one table and insert into another table
- Copy column values from one table into another matching IDs in MySQL
- Can we add a column to a table from another table in MySQL?
- Updating a MySQL table with values from another table?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- How to copy a table in MySQL using Python?
- Need help in deleting duplicate columns from a table in MySQL?
- MySQL select query to select rows from a table that are not in another table?
- How to add a row to a table using only strings from another table as reference in MySQL?
- MySQL command to copy table?

Advertisements