
- 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 combinations from two columns in MySQL?
To select distinct combinations from two columns, you can use CASE statement. Let us create a table with some columns.
The query to create a table is as follows −
mysql> create table select_DistinctTwoColumns -> ( -> Id int NOT NULL AUTO_INCREMENT, -> FirstValue char(1), -> SecondValue char(1), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.57 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into select_DistinctTwoColumns(FirstValue,SecondValue) values('s','t'); Query OK, 1 row affected (0.12 sec) mysql> insert into select_DistinctTwoColumns(FirstValue,SecondValue) values('t','u'); Query OK, 1 row affected (0.24 sec) mysql> insert into select_DistinctTwoColumns(FirstValue,SecondValue) values('u','v'); Query OK, 1 row affected (0.12 sec) mysql> insert into select_DistinctTwoColumns(FirstValue,SecondValue) values('u','t'); 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 select_DistinctTwoColumns;
The following is the output −
+----+------------+-------------+ | Id | FirstValue | SecondValue | +----+------------+-------------+ | 1 | s | t | | 2 | t | u | | 3 | u | v | | 4 | u | t | +----+------------+-------------+ 4 rows in set (0.00 sec)
Here is the query to select distinct combinations from two column using case statement. The first column is ‘FirstValue’ and the second column name is ‘SecondValue’. The query is as follows −
mysql> SELECT distinct -> CASE -> WHEN FirstValue<SecondValue THEN FirstValue -> ELSE SecondValue -> END AS FirstColumn, -> CASE -> WHEN FirstValue > SecondValue THEN FirstValue -> ELSE SecondValue -> END AS SecondColumn -> FROM select_DistinctTwoColumns;
The following is the output:
+-------------+--------------+ | FirstColumn | SecondColumn | +-------------+--------------+ | s | t | | t | u | | u | v | +-------------+--------------+ 3 rows in set (0.00 sec)
- Related Articles
- MYSQL select DISTINCT values from two columns?
- Select distinct values from two columns in MySQL?
- Select distinct names from two columns in MySQL and display the result in a single column
- MySQL Select Statement DISTINCT for Multiple Columns?
- SELECT not null column from two columns in MySQL?
- Select distinct values from three columns and display in a single column with MySQL
- Select and add result of multiplying two columns from a table in MySQL?
- Select maximum of sum of two columns in MySQL
- MySQL select distinct dates from datetime column in a table?
- MySQL SELECT DISTINCT and count?
- MySQL select * with distinct id?
- How to select distinct value from one MySQL column only?
- MySQL query to select average from distinct column of table?
- SELECT DISTINCT vs GROUP BY in MySQL?
- Return similar names from different columns with distinct space allocations in MySQL?

Advertisements