
- 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
Perform complex MySQL insert by using CONCAT()?
To perform complex MySQL insert, you can use CONCAT() function. Let us see an example and create a table with StudentId and StudentFirstName.
After that, complex MySQL insert will be performed and 'Web Student’ text will get inserted for every value and unique StudentId will get concatenated.
The query to create first table is as follows −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(20) ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentFirstName) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTableStudentFirstName) values('Larry'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentFirstName) values('Bob'); Query OK, 1 row affected (0.13 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+ | StudentId | StudentFirstName | +-----------+------------------+ | 1 | John | | 2 | Larry | | 3 | Bob | +-----------+------------------+ 3 rows in set (0.00 sec)
Here is the query to create second table which is as follows −
mysql> create table DemoTable2 ( ClientId int, ClientProjectName varchar(20) ); Query OK, 0 rows affected (0.54 sec)
Following is the complex MySQL insert −
mysql> insert into DemoTable2 select StudentId,concat('Web Student=', StudentId) from DemoTable; Query OK, 3 rows affected (0.17 sec) Records : 3 Duplicates : 0 Warnings : 0
Display all records from the second table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+----------+-------------------+ | ClientId | ClientProjectName | +----------+-------------------+ | 1 | Web Student=1 | | 2 | Web Student=2 | | 3 | Web Student=3 | +----------+-------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to group concat distinct by Id?
- What MySQL CONCAT() function returns by passing the numeric arguments?
- Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
- Insert record using MySQL SELECT?
- Perform MySQL ORDER BY keyword match?
- Perform Bulk insert in MongoDB?
- Add to existing value in MySQL column using CONCAT function?
- C++ Program to Perform Complex Number Multiplication
- String Concatenation by concat() method.
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- Perform case insensitive SELECT using MySQL IN()?
- Perform multiple counting without using MySQL COUNT()?
- Concat a field in MySQL SELECT?
- Use MySQL concat() and lower() effectively
- How can we set up a MySQL User account by using INSERT INTO statement?

Advertisements