
- 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
Concat a field in MySQL SELECT?
To concat a field in MySQL SELECT, use the CONCAT() function as shown in the below syntax.
SELECT CONCAT(yourColumnName1,’anyConcatenationString’),CONCAT(yourColumnName2,’anyC oncatenationString’),....N from yourTableName;
To understand the above syntax, let us first create a table. The query to create a table is as follows.
mysql> create table selectConcat -> ( -> StudentId int, -> StudentName varchar(100), -> StudentAge int -> ); Query OK, 0 rows affected (1.32 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into selectConcat values(1,'Carol',23); Query OK, 1 row affected (0.19 sec) mysql> insert into selectConcat values(2,'John',24); Query OK, 1 row affected (0.25 sec) mysql> insert into selectConcat values(3,'Adam',25); Query OK, 1 row affected (0.14 sec) mysql> insert into selectConcat values(4,'Bob',21); Query OK, 1 row affected (0.20 sec) mysql> insert into selectConcat values(5,'Sam',22); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from selectConcat;
The following is the output.
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Carol | 23 | | 2 | John | 24 | | 3 | Adam | 25 | | 4 | Bob | 21 | | 5 | Sam | 22 | +-----------+-------------+------------+ 5 rows in set (0.00 sec)
Contact the field in select statement. The query is as follows.
mysql> select concat(StudentId,' as an Id'),concat(StudentName,' as a Name') from selectConcat;
The following is the output.
+-------------------------------+----------------------------------+ | concat(StudentId,' as an Id') | concat(StudentName,' as a Name') | +-------------------------------+----------------------------------+ | 1 as an Id | Carol as a Name | | 2 as an Id | John as a Name | | 3 as an Id | Adam as a Name | | 4 as an Id | Bob as a Name | | 5 as an Id | Sam as a Name | +-------------------------------+----------------------------------+ 5 rows in set (0.00 sec)
- Related Articles
- Concat a string to SELECT * in MySQL?
- How to generate field in MySQL SELECT?
- Select a field and if it's null, select another with MySQL?
- Limit length of longtext field in MySQL SELECT results?
- How to select data in MySQL where a field has a minimum value?
- Select different fields in MySQL even if a field is set to null?
- Return true or false in a MySQL select if another field contains a string?
- Can we select field name in MySQL that contains an asterisk?
- Can we use “LIKE concat()” in a MySQL query?
- MySQL select any one field out of two with respect to the value of a third field?
- Getting last value in MySQL group concat?
- Set an alternative of WHERE clause for each SELECT field in MySQL
- How to map keys to values for an individual field in a MySQL select query?
- How to use GROUP_CONCAT in CONCAT in MySQL?
- How to select a single field in MongoDB?

Advertisements