
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Select Multiple Columns and Display in a Single Column in MySQL
Use concat() for this. Let us first create a table −
mysql> create table DemoTable -> ( -> FirstName varchar(30), -> LastName varchar(30) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris | Brown | | Adam | Smith | | Carol | Taylor | +-----------+----------+ 3 rows in set (0.00 sec)
Following is the query to select multiple columns with single alias −
mysql> select concat(FirstName,' ',LastName) as concatValue from DemoTable order by concatValue DESC;
Output
This will produce the following output −
+--------------+ | concatValue | +--------------+ | Chris Brown | | Carol Taylor | | Adam Smith | +--------------+ 3 rows in set (0.00 sec)
Advertisements