
- 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
MySQL Select Statement DISTINCT for Multiple Columns?
To understand the MySQL select statement DISTINCT for multiple columns, let us see an example and create a table. The query to create a table is as follows
mysql> create table selectDistinctDemo -> ( -> InstructorId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentId int, -> TechnicalSubject varchar(100) -> ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command. The query is as follows
mysql> insert into selectDistinctDemo(StudentId,TechnicalSubject) values(121,'Java'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectDistinctDemo(StudentId,TechnicalSubject) values(121,'MongoDB'); Query OK, 1 row affected (0.16 sec) mysql> insert into selectDistinctDemo(StudentId,TechnicalSubject) values(121,'MySQL'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectDistinctDemo(StudentId,TechnicalSubject) values(298,'Python'); Query OK, 1 row affected (0.11 sec) mysql> insert into selectDistinctDemo(StudentId,TechnicalSubject) values(298,'SQL Server'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectDistinctDemo(StudentId,TechnicalSubject) values(397,'C#'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using a select statement. The query is as follows
mysql> select *from selectDistinctDemo;
The following is the output
+--------------+-----------+------------------+ | InstructorId | StudentId | TechnicalSubject | +--------------+-----------+------------------+ | 1 | 121 | Java | | 2 | 121 | MongoDB | | 3 | 121 | MySQL | | 4 | 298 | Python | | 5 | 298 | SQL Server | | 6 | 397 | C# | +--------------+-----------+------------------+ 6 rows in set (0.00 sec)
Here is the query to use select statement DISTINCT for multiple columns
mysql> select InstructorId,StudentId,TechnicalSubject from selectDistinctDemo -> where InstructorId IN -> ( -> select max(InstructorId) from selectDistinctDemo -> group by StudentId -> ) -> order by InstructorId desc;
The following is the output
+--------------+-----------+------------------+ | InstructorId | StudentId | TechnicalSubject | +--------------+-----------+------------------+ | 6 | 397 | C# | | 5 | 298 | SQL Server | | 3 | 121 | MySQL | +--------------+-----------+------------------+ 3 rows in set (0.10 sec)
- Related Articles
- MYSQL select DISTINCT values from two columns?
- Select distinct values from two columns in MySQL?
- Select distinct combinations from two columns in MySQL?
- How to use MySQL DISTINCT clause on multiple columns?
- MySQL alias for SELECT * columns?
- Explain the use of SELECT DISTINCT statement in MySQL using Python?
- How to alter multiple columns in a single statement in MySQL?
- Select multiple columns and display in a single column in MySQL?
- MySQL SELECT DISTINCT and count?
- MySQL select * with distinct id?
- Count value for multiple columns in MySQL?\n
- Set multiple values for custom columns in MySQL?
- Select distinct values from three columns and display in a single column with MySQL
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- Select multiple sums with MySQL query and display them in separate columns?

Advertisements