
- 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
Select and insert values with preceding zeros in a MySQL table
For this, you can use INSERT INTO SELECT statement along with LPAD(). Let us first create a table −
mysql> create table DemoTable1967 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserId varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1967(UserId) select LPAD(COALESCE(MAX(id), 0) + 1, 3, '0') from DemoTable1967; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into DemoTable1967(UserId) select LPAD(COALESCE(MAX(id), 0) + 1, 3, '0') from DemoTable1967; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into DemoTable1967(UserId) select LPAD(COALESCE(MAX(id), 0) + 1, 3, '0') from DemoTable1967; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1967;
This will produce the following output −
+----+--------+ | Id | UserId | +----+--------+ | 1 | 001 | | 2 | 002 | | 3 | 003 | +----+--------+ 3 rows in set (0.00 sec)
- Related Articles
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Combine INSERT, VALUES, and SELECT in MySQL
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- How to insert an array of values in a MySQL table with a single INSERT?
- Insert with a Select query in MySQL
- Insert multiple values in a temporary table with a single MySQL query?
- Select some data from a database table and insert into another table in the same database with MySQL
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- Auto insert values into a MySQL table in a range?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- MySQL select and insert in two tables with a single query
- Insert record in a MySQL table with Java
- How to SELECT fields from one table and INSERT to another in MySQL?
- Can we insert records in a MySQL table without auto_increment values?
- A single MySQL query to select value from first table and insert in the second?

Advertisements