
- 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 values from three columns and display in a single column with MySQL
For this, use UNION more than once in a single MySQL query. Let us first create a table −
mysql> create table DemoTable -> ( -> Value1 int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(20,null,null); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(20,null,null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(20,null,null); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(10,null,null); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(80,20,100); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(10,null,null); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 20 | NULL | NULL | | 20 | NULL | NULL | | 20 | NULL | NULL | | 10 | NULL | NULL | | 80 | 20 | 100 | | 10 | NULL | NULL | +--------+--------+--------+ 6 rows in set (0.00 sec)
Following is the query to select distinct values from 3 columns into 1 −
mysql> select *from -> ( -> select Value1 as AllValue from DemoTable -> union -> select Value2 as AllValue from DemoTable -> union -> select Value3 as AllValue from DemoTable -> ) tbl where AllValue IS NOT NULL -> order by AllValue;
This will produce the following output −
+----------+ | AllValue | +----------+ | 10 | | 20 | | 80 | | 100 | +----------+ 4 rows in set (0.00 sec)
- Related Articles
- Select distinct names from two columns in MySQL and display the result in a single column
- Select multiple columns and display in a single column in MySQL?
- MYSQL select DISTINCT values from two columns?
- Select distinct values from two columns in MySQL?
- How to select different values from same column and display them in different columns with MySQL?
- Concatenate the column values with separate text in MySQL and display in a single column
- Display the sum of positive and negative values from a column in separate columns with MySQL
- Select distinct combinations from two columns in MySQL?
- Display distinct dates in MySQL from a column with date records
- MySQL select distinct dates from datetime column in a table?
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- How to quote values of single column using GROUP_CONCAT and CONCAT with DISTINCT in MySQL?
- Divide numbers from two columns and display result in a new column with MySQL
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
- Multiply values of two columns and display it a new column in MySQL?

Advertisements