
- 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
Insert multiple rows from another table but the inserted records should be distinct
For this, you can use DISTINCT along with the INSERT INTO SELECT statement. Let us first create a table −
mysql> create table DemoTable1 ( Value int ); Query OK, 0 rows affected (1.03 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(50); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 values(60); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1 values(50); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1 values(70); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 values(50); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+-------+ | Value | +-------+ | 50 | | 10 | | 10 | | 60 | | 50 | | 70 | | 50 | +-------+ 7 rows in set (0.00 sec)
Following is the query to create the second table.
mysql> create table DemoTable2 ( Marks int ); Query OK, 0 rows affected (1.20 sec)
Following is the query to insert multiple rows from another table. The inserted records should be distinct −
mysql> insert into DemoTable2(Marks) select distinct Value from DemoTable1; Query OK, 4 rows affected (0.18 sec) Records: 4 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+-------+ | Marks | +-------+ | 50 | | 10 | | 60 | | 70 | +-------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- Take all records from one MySQL table and insert it to another?
- Insert records from multiple tables in MySQL
- How to insert multiple rows in a table using a single INSERT command in program?
- MongoDB query to insert but limit the total records
- Insert data from one table to another in MySQL?
- MySQL query to insert multiple records quickly
- Move rows from one table to another in MySQL?
- MySQL query for INSERT INTO using values from another table?
- A single MySQL query to insert records (not all) in the second table from the first table
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL statement to copy data from one table and insert into another table
- Insert current time minus 1 hour to already inserted date-time records in MYSQL
- How can we delete multiple rows from a MySQL table?
- MySQL query to insert data from another table merged with constants?

Advertisements