
- 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
How to select distinct value from one MySQL column only?
To select distinct value from one column only, you can use aggregate function MAX() along with GROUP BY. Let us first create a table −
mysql> create table distinctFromOneColumn -> ( -> StudentId int, -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.77 sec)
Following is the query to insert records in the table using insert command −
mysql> insert into distinctFromOneColumn values(1001,'John'); Query OK, 1 row affected (0.15 sec) mysql> insert into distinctFromOneColumn values(1002,'Carol'); Query OK, 1 row affected (0.15 sec) mysql> insert into distinctFromOneColumn values(1001,'Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into distinctFromOneColumn values(1001,'David'); Query OK, 1 row affected (0.16 sec) mysql> insert into distinctFromOneColumn values(1002,'Ramit'); Query OK, 1 row affected (0.15 sec) mysql> insert into distinctFromOneColumn values(1003,'Bob'); Query OK, 1 row affected (0.21 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from distinctFromOneColumn;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1001 | John | | 1002 | Carol | | 1001 | Sam | | 1001 | David | | 1002 | Ramit | | 1003 | Bob | +-----------+-------------+ 6 rows in set (0.00 sec)
Here is the query to select a distinct value from one column only −
mysql> select StudentId,MAX(StudentName) AS StudentName -> from distinctFromOneColumn -> group by StudentId;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1001 | Sam | | 1002 | Ramit | | 1003 | Bob | +-----------+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to select only MySQL date from datetime column?
- MySQL query to select average from distinct column of table?
- MySQL select distinct dates from datetime column in a table?
- How to select only non - numeric values from varchar column in MySQL?
- MySQL query to select column where value = one or value = two, value = three, etc?
- MySQL query to select rows where column value is only 0, group by another column?
- Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values
- MySQL select only a single value from 5 similar values?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- MYSQL select DISTINCT values from two columns?
- Select distinct values from three columns and display in a single column with MySQL
- Replace only a specific value from a column in MySQL
- MySQL select distinct rows into a comma delimited list column?
- Select distinct values from two columns in MySQL?
- Select distinct combinations from two columns in MySQL?

Advertisements