
- 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
Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
Let us first create a table −
mysql> create table DemoTable1 ( Id int, Name varchar(100) ); Query OK, 0 rows affected (0.86 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(1001,'Chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 values(999,'Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values(1003,'Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1 values(1002,'Sam'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+------+--------+ | Id | Name | +------+--------+ | 1001 | Chris | | 999 | Robert | | 1003 | Mike | | 1002 | Sam | +------+--------+ 4 rows in set (0.00 sec)
Following is the query to create the second table −
mysql> create table DemoTable2 ( StudentId int, StudentFirstName varchar(100) ); Query OK, 0 rows affected (1.15 sec)
Insert some records in the table using insert command. Here, we have inserted the maximum ID value from the first table to the StudentID column of the second table −
mysql> insert into DemoTable2(StudentId,StudentFirstName) select (select Max(Id) from DemoTable1), Name from DemoTable1; Query OK, 4 rows affected (0.20 sec) Records: 4 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+-----------+------------------+ | StudentId | StudentFirstName | +-----------+------------------+ | 1003 | Chris | | 1003 | Robert | | 1003 | Mike | | 1003 | Sam | +-----------+------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Select some data from a database table and insert into another table in the same database with MySQL
- A single MySQL query to select value from first table and insert in the second?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- 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
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- How to SELECT fields from one table and INSERT to another in MySQL?
- MySQL trigger to insert row into another table?
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- MySQL query for INSERT INTO using values from another table?
- Take all records from one MySQL table and insert it to another?
- A single MySQL query to insert records (not all) in the second table from the first table
- Insert from one table with different structure to another in MySQL?
- Insert data from one table to another in MySQL?
- Fetch the first letter of a column value and insert it in another column with MySQL

Advertisements