- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to select and display a list of values in one column that are available in two different MySQL columns?
For this, use UNION ALL. Let us first create a table −
mysql> create table DemoTable1813 ( Name1 varchar(20), Name2 varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1813 values('John','Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1813 values('Adam','Robert'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1813 values('Mike','Sam'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1813;
This will produce the following output −
+-------+--------+ | Name1 | Name2 | +-------+--------+ | John | Chris | | Adam | Robert| | Mike | Sam | +-------+--------+ 3 rows in set (0.00 sec)
Here is the query to select a list of values in one column that are available in two different columns
mysql> select Name1 as Name from DemoTable1813 union all select Name2 as Name from DemoTable1813;
This will produce the following output −
+--------+ | Name | +--------+ | John | | Adam | | Mike | | Chris | | Robert | | Sam | +--------+ 6 rows in set (0.00 sec)
- Related Articles
- How to select different values from same column and display them in different columns with MySQL?
- Count only null values in two different columns and display in one MySQL select statement?
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- Multiply values of two columns and display it a new column in MySQL?
- Select distinct values from three columns and display in a single column with MySQL
- Select multiple columns and display in a single column in MySQL?
- Select distinct names from two columns in MySQL and display the result in a single column
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- Concatenate two columns when one of such column values is null in MySQL
- Select distinct values from two columns in MySQL?
- SELECT not null column from two columns in MySQL?
- MYSQL select DISTINCT values from two columns?
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
- Select a specific value between two column values in MySQL?
- Select values that meet different conditions on different rows in MySQL?

Advertisements