
- 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
Select distinct names from two columns in MySQL and display the result in a single column
For this, use UNION. Let us first create a table −
mysql> create table DemoTable ( Name1 varchar(100), Name2 varchar(100) ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command
mysql> insert into DemoTable values('Adam','Bob'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Adam','Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David','Chris'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------+ | Name1 | Name2 | +-------+-------+ | Adam | Bob | | Adam | Bob | | David | Chris | +-------+-------+ 3 rows in set (0.00 sec)
Following is the query to select distinct names from two columns −
mysql> select Name1 from DemoTable UNION select Name2 from DemoTable;
This will produce the following output −
+-------+ | Name1 | +-------+ | Adam | | David | | Bob | | Chris | +-------+ 4 rows in set (0.02 sec)
- Related Articles
- 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?
- Divide numbers from two columns and display result in a new column with MySQL
- Select distinct values from two columns in MySQL?
- Select distinct combinations from two columns in MySQL?
- MYSQL select DISTINCT values from two columns?
- Select and add result of multiplying two columns from a table in MySQL?
- MySQL query to get the length of all columns and display the result in a single new column?
- SELECT not null column from two columns in MySQL?
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- Get the highest score value from a single column and the greatest from two columns in MySQL
- How to separate last name and first names in single column into two new columns in MySQL?
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- MySQL query to count occurrences of distinct values and display the result in a new column?

Advertisements