

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to concatenate strings from different columns and an additional string using a MySQL query?
<p>Let us first create a table −</p><pre class="result notranslate">mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (0.76 sec)</pre><p>Insert some records in the table using insert command −</p><pre class="result notranslate">mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.12 sec)</pre><p>Display all records from the table using select statement −</p><pre class="result notranslate">mysql> select *from DemoTable;</pre><p>This will produce the following output −</p><pre class="result notranslate">+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris | Brown | | Adam | Smith | | Carol | Taylor | | David | Miller | +-----------+----------+ 4 rows in set (0.00 sec)</pre><p>Following is the query to perform concatenation. Here, we have FirstName and LastName. With that an additional string is also concatenated in the beginning for all the string values −</p><pre class="result notranslate">mysql> select concat('Hello ',FirstName,' ',LastName) from DemoTable;</pre><p>This will produce the following output −</p><pre class="result notranslate">+-----------------------------------------+ | concat('Hello ',FirstName,' ',LastName) | +-----------------------------------------+ | Hello Chris Brown | | Hello Adam Smith | | Hello Carol Taylor | | Hello David Miller | +-----------------------------------------+ 4 rows in set (0.00 sec)</pre>
- Related Questions & Answers
- Concatenate columns from different tables in MySQL
- How to concatenate strings using both GROUP_CONCAT() and CONCAT() in the same MySQL query?
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- How to concatenate MySQL distinct query results into a string?
- Different ways to concatenate Strings in Java
- How to concatenate all columns in MySQL?
- How to concatenate two strings using Java?
- MySQL query to copy records from one table to another with different columns
- How MySQL stored GENERATED COLUMNS are different from MySQL virtual GENERATED COLUMNS?
- Concatenate date and time from separate columns into a single column in MySQL
- Can MySQL concatenate strings with ||?
- Count two different columns in a single query in MySQL?
- Concatenate two columns in MySQL?
- How to concatenate a std::string and an int in C++?
- How to select different values from same column and display them in different columns with MySQL?
Advertisements