- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adding/ concatenating text values within a MySQL SELECT clause?
To add/ concatenate text values within a select clause, you can use concat() function.
Let us create a table
mysql> create table ConcatenatingDemo -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserName varchar(20), -> UserCountryName varchar(20) -> ); Query OK, 0 rows affected (0.82 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into ConcatenatingDemo(UserName,UserCountryName) values('John','US'); Query OK, 1 row affected (0.14 sec) mysql> insert into ConcatenatingDemo(UserName,UserCountryName) values('Carol','UK'); Query OK, 1 row affected (0.13 sec) mysql> insert into ConcatenatingDemo(UserName,UserCountryName) values('Bob','AUS'); Query OK, 1 row affected (0.15 sec) mysql> insert into ConcatenatingDemo(UserName,UserCountryName) values('David','US'); Query OK, 1 row affected (0.29 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from ConcatenatingDemo;
The following is the output
+--------+----------+-----------------+ | UserId | UserName | UserCountryName | +--------+----------+-----------------+ | 1 | John | US | | 2 | Carol | UK | | 3 | Bob | AUS | | 4 | David | US | +--------+----------+-----------------+ 4 rows in set (0.00 sec)
Here is the query for adding/ concatenating text values within a SELECT clause
mysql> select concat(UserName,' belongs to ( ',UserCountryName,' )') as AddingTextDemo from ConcatenatingDemo;
The following is the output
+-------------------------+ | AddingTextDemo | +-------------------------+ | John belongs to ( US ) | | Carol belongs to ( UK ) | | Bob belongs to ( AUS ) | | David belongs to ( US ) | +-------------------------+ 4 rows in set (0.00 sec)
Advertisements